如何使用asynctask在android中加载图像缩略图

时间:2014-02-11 07:13:18

标签: android image android-asynctask imageview bitmapimage

我正在尝试从服务器获取超过10个视频的缩略图到android中的Image-view。现在我能够成功地将缩略图的视频显示到图像视图中,但我的问题是显示所有缩略图需要花费太多时间。所以它显示空白的黑屏直到它加载所有缩略图。

所以我决定使用Asynctask显示进度条,直到它加载所有缩略图,但它根本不显示进度条。所以任何想法如何解决这个问题。

代码

ImageView video_one, video_two, video_three, video_four, video_five;
ImageView video_six, video_seven, video_eight, video_nine, video_ten;
ImageView video_ele, video_twe, video_thir, video_fort, video_fif;

    String path  = "http://serverlink/Fidol/upload/test.mp4";
    String path1 = "http://serverlink/Fidol/upload/ABCD.mp4";
    String path2 = "http://serverlink/Fidol/upload/bean.mp4";
    String path3 = "http://serverlink/Fidol/upload/masti.mp4";
    String path4 = "http://serverlink/Fidol/upload/ben.mp4";

        Bitmap bm = ThumbnailUtils.createVideoThumbnail(path,
            MediaStore.Images.Thumbnails.MICRO_KIND);
    Bitmap bm1 = ThumbnailUtils.createVideoThumbnail(path1,
            MediaStore.Images.Thumbnails.MICRO_KIND);
    Bitmap bm2 = ThumbnailUtils.createVideoThumbnail(path2,
            MediaStore.Images.Thumbnails.MICRO_KIND);
    Bitmap bm3 = ThumbnailUtils.createVideoThumbnail(path3,
            MediaStore.Images.Thumbnails.MICRO_KIND);
    Bitmap bm4 = ThumbnailUtils.createVideoThumbnail(path4,
            MediaStore.Images.Thumbnails.MICRO_KIND);
然后我打电话给

new loadThumbnail().execute();

这是我的asynctask类

class loadThumbnail extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ShowVideo.this);
            pDialog.setMessage("Loading Videos... Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(String... args) {

            video_one.setImageBitmap(bm);
            video_two.setImageBitmap(bm1);
            video_three.setImageBitmap(bm2);
            video_four.setImageBitmap(bm3);
            video_five.setImageBitmap(bm4);

            video_six.setImageBitmap(bm);
            video_seven.setImageBitmap(bm1);
            video_eight.setImageBitmap(bm2);
            video_nine.setImageBitmap(bm3);
            video_ten.setImageBitmap(bm4);

            video_ele.setImageBitmap(bm);
            video_twe.setImageBitmap(bm1);
            video_thir.setImageBitmap(bm2);
            video_fort.setImageBitmap(bm3);
            video_fif.setImageBitmap(bm4);

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
        }
    }

5 个答案:

答案 0 :(得分:1)

这样做:

            video_one.setImageBitmap(bm);
            video_two.setImageBitmap(bm1);
            video_three.setImageBitmap(bm2);
            video_four.setImageBitmap(bm3);
            video_five.setImageBitmap(bm4);

            video_six.setImageBitmap(bm);
            video_seven.setImageBitmap(bm1);
            video_eight.setImageBitmap(bm2);
            video_nine.setImageBitmap(bm3);
            video_ten.setImageBitmap(bm4);

            video_ele.setImageBitmap(bm);
            video_twe.setImageBitmap(bm1);
            video_thir.setImageBitmap(bm2);
            video_fort.setImageBitmap(bm3);
            video_fif.setImageBitmap(bm4);

在onPostExecute中,因为你无法从doinbackground修改ui

并在doInbackground中执行此操作

        Bitmap bm = ThumbnailUtils.createVideoThumbnail(path,
        MediaStore.Images.Thumbnails.MICRO_KIND);
        Bitmap bm1 = ThumbnailUtils.createVideoThumbnail(path1,
        MediaStore.Images.Thumbnails.MICRO_KIND);
        Bitmap bm2 = ThumbnailUtils.createVideoThumbnail(path2,
        MediaStore.Images.Thumbnails.MICRO_KIND);
        Bitmap bm3 = ThumbnailUtils.createVideoThumbnail(path3,
        MediaStore.Images.Thumbnails.MICRO_KIND);
        Bitmap bm4 = ThumbnailUtils.createVideoThumbnail(path4,
        MediaStore.Images.Thumbnails.MICRO_KIND);

答案 1 :(得分:0)

我认为你应该把

Bitmap bm = ThumbnailUtils.createVideoThumbnail(path,
        MediaStore.Images.Thumbnails.MICRO_KIND);
Bitmap bm1 = ThumbnailUtils.createVideoThumbnail(path1,
        MediaStore.Images.Thumbnails.MICRO_KIND);
Bitmap bm2 = ThumbnailUtils.createVideoThumbnail(path2,
        MediaStore.Images.Thumbnails.MICRO_KIND);
Bitmap bm3 = ThumbnailUtils.createVideoThumbnail(path3,
        MediaStore.Images.Thumbnails.MICRO_KIND);
Bitmap bm4 = ThumbnailUtils.createVideoThumbnail(path4,
        MediaStore.Images.Thumbnails.MICRO_KIND);

doInBackground方法下,因为据我所知,这是任务发生的地方。

答案 2 :(得分:0)

在AsyncTask中有一个方法调用onProgressUpdate(Integer...),您可以将其用于进度更新。 Check this link

答案 3 :(得分:0)

试试这个

@Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(ShowVideo.this);
        pDialog.setMessage("Loading Videos... Please wait...");
        pDialog.show();
    }

同样在你的代码中,如果你已经在某处删除了loadThumbnail.get(),那么这将阻止UI线程。

答案 4 :(得分:0)

这可能会有所帮助:

class SetThumb extends AsyncTask<String, Void, Bitmap>{
private ImageView imageView;
SetThumb(ImageView imageView){
    this.imageView=imageView;
}

@Override
protected Bitmap doInBackground(String... strings) {
    return ThumbnailUtils.createVideoThumbnail(strings[0],MediaStore.Video.Thumbnails.MICRO_KIND);
}

@Override
protected void onPostExecute(Bitmap thumb){
    imageView.setImageBitmap(thumb);
}

}

现在执行此任务

new SetThumb(your_image_view).execute(path_of_file);