I am trying to get thumbnail of more then 10 videos from server to Image-view in android. Now i am successfully able to display thumbnail's of videos into image-view but my problem is it is taking too much time to display all thumbnail's. So it is showing blank black screen till it load all thumbnail's.

So i decided to use Asynctask that show's progress bar till it load's all thumbnail's but it doesn't show progress bar at all. So any idea how to solve this issue.

Code

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);

then i have called

new loadThumbnail().execute();

and here is my asynctask class

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();
        }
    }
有帮助吗?

解决方案

do this :

            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);

in onPostExecute becuase you can not modify ui from doinbackground

and do this in 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);

其他提示

I think you should put

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);

under the doInBackground method because as far as I understand thats where the task is happening.

In AsyncTask there's a method callonProgressUpdate(Integer...) which you can make use for progress updating. Check this link

Try this

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

Also in your code if you have written loadThumbnail.get() somewhere remove that too,as this will block the UI thread.

This might help:

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);
}

}

NOW EXECUTE THIS TASK

new SetThumb(your_image_view).execute(path_of_file);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top