Question

I have implemented an Async Task which creates thumbnails for a video Playlist. During the background thread I have implemented a Progress dialog which is triggered in the PreExecute Task. At the moment am dismissing the dialog on the postExecute method but i must be missing something. Can I have some help on That?

My code is below

VideoListActivity.java

package com.oneplc.viessmannapp;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;



import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import wseemann.media.FFmpegMediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.widget.GridView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class VideoListActivity extends Activity {

    private static ProgressDialog pDialog;

    private static String MEDIA_PATH;

    String[] fileList = null;

    ListView mList;
    String FILE_PATH;
    String MIME_TYPE = "video/mp4";

    private ImageView goBack;
    private static Context mContext;

    private ImageAdapter mAdapter;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mAdapter.notifyDataSetChanged();                       
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);

        goBack = (ImageView) findViewById(R.id.back_btn);
        mContext = this;
        setActionListeners();

        String folder = getIntent().getStringExtra("folder");

        MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath() + "/"+folder+"/");
        updateVideoList();
        FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/"+folder+"/";

        mList = (ListView) findViewById(R.id.listView1);
        if(fileList !=null){
            mAdapter = new ImageAdapter(this, fileList);
            mList.setAdapter(mAdapter);
        }
        mList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Toast.makeText(
                        getApplicationContext(),
                        ((TextView) v.findViewById(R.id.item_title))
                        .getText(), Toast.LENGTH_SHORT).show();
                String videoFilePath = FILE_PATH + fileList[position];
                System.out.println("******************************videoFilePath****************" + videoFilePath);
                System.out.println("******************************MiME_TYPE****************"+ MIME_TYPE);
                //                Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
                //                File newFile = new File(videoFilePath);
                //                intent.setDataAndType(Uri.fromFile(newFile), MiME_TYPE);
                //                startActivity(intent);
                Intent intent = new Intent(VideoListActivity.this, VideoViewActivity.class);
                intent.putExtra("videopath", videoFilePath);
                startActivity(intent);
            }
        });

    }

    private void setActionListeners() {
        // TODO Auto-generated method stub
        goBack.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                //Clear up VM heap stack
                System.gc();
                finish();
            }
        });
    }

    public static String FormatDate(Date yourDate) {

        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss",Locale.US);

        return sdf.format(yourDate);

    }

    public void updateVideoList() {
        File videoFiles = new File(MEDIA_PATH);
        Log.d("*********Value of videoFiles******", videoFiles.toString());
        System.out.println(MEDIA_PATH);
        if (videoFiles.isDirectory()) {
            fileList = videoFiles.list();
        }
        if (fileList == null) {
            System.out.println("File doesnot exit");
            Toast.makeText(this, "There is no file", Toast.LENGTH_SHORT).show();
        } else {
            System.out.println("fileList****************" + fileList);
            for (int i = 0; i < fileList.length; i++) {
                Log.e("Video:" + i + " File name", fileList[i]);
            }
        }
    }

    public static Bitmap scaleBimtap(Bitmap bitmap, int width, int height) {
        final int bitmapWidth = bitmap.getWidth();
        final int bitmapHeight = bitmap.getHeight();

        final float scale = Math.min((float) width / (float) bitmapWidth,
                (float) height / (float) bitmapHeight);

        final int scaledWidth = (int) (bitmapWidth * scale);
        final int scaledHeight = (int) (bitmapHeight * scale);

        final Bitmap decoded = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);

        return decoded;
    }

    private class ProcessImageTask extends AsyncTask<Void, Void, Bitmap> {

        private Handler mHandler;
        private ImageView mImageView;
        private String mUri;

        public ProcessImageTask(Handler handler, ImageView imageView, String uri) {
            mHandler = handler;
            mImageView = imageView;
            mUri = uri;
        }

        protected void onPreExecute(){
            super.onPreExecute();
            pDialog = new ProgressDialog(mContext);
            pDialog.setMessage("Loading Playlist...");
            pDialog.show();
        }

        @Override
        protected Bitmap doInBackground(Void... v) {
            FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
            mmr.setDataSource(mUri);

            //here you change the thumbnail frame
            Bitmap bmThumbnail = mmr.getFrameAtTime(2000000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST); // frame at 2 seconds
            if(bmThumbnail !=null){
                scaleBimtap(bmThumbnail, 20, 20);

            }
            mmr.release();
            //-----------------
            return bmThumbnail;
        }

        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            //-----------------
            if (bitmap != null) {
                System.out
                .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                mImageView.setImageBitmap(bitmap);
                mHandler.sendEmptyMessage(0);

            } else {
                System.out
                .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>NO THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            }
            pDialog.dismiss();
        }
    }

    public class ImageAdapter extends BaseAdapter {
        private final Context context;
        private final String[] VideoValues;

        public ImageAdapter(Context context, String[] VideoValues) {
            this.context = context;
            this.VideoValues = VideoValues;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            System.out.println("***********IngetView************");
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View listview;

            if (convertView == null) {

                listview = new View(context);    
                listview = inflater.inflate(R.layout.gridlayout, null);          
                TextView textView = (TextView) listview
                        .findViewById(R.id.item_title);      
                TextView tv_time = (TextView) listview
                        .findViewById(R.id.tv_time);

                textView.setText(fileList[position]);
                System.out.println("value of fileList[position]" + fileList[0]);
                // set image
                ImageView imageThumbnail = (ImageView) listview
                        .findViewById(R.id.item_image);
                int msec = MediaPlayer.create(context, Uri.fromFile(new File(FILE_PATH + fileList[position]))).getDuration();

                tv_time.setText(FormatDate(new Date(msec)));
                System.out
                .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> file path>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
                        + fileList[position]);
                /*FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
                                mmr.setDataSource(FILE_PATH + fileList[position]);

                                //here you change the thumbnail frame
                                Bitmap bmThumbnail = mmr.getFrameAtTime(2000000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST); // frame at 2 seconds

                                mmr.release();
                                //-----------------
                                if (bmThumbnail != null) {
                                        System.out
                                        .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                                        imageThumbnail.setImageBitmap(bmThumbnail);
                                } else {
                                        System.out
                                        .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>NO THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                                }*/

                new ProcessImageTask(mHandler, imageThumbnail, FILE_PATH + fileList[position]).execute();

            } else {
                listview = convertView;
            }
            return listview;
        }
        @Override
        public int getCount() {
            // return 0;
            return VideoValues.length;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
    }


}
Was it helpful?

Solution

The problem is that you're creating new ProgressDialogs, over and over again, and losing the reference to the old ones.

Since I'm not seeing pDialog declared anywhere within your AsyncTask, I'm guessing its external. On the other hand, you are obviously creating multiple AsyncTasks to deal with multiple images. This means that you are running multiple times the code:

pDialog = new ProgressDialog(mContext);

Each time, losing the older ProgressDialog.


Suggested solution:

Add a counter, along with the pDialog:

private static ProgressDialog pDialog;
private static int pDialogCounter = 0;

Then change onPreExecute() to:

protected void onPreExecute(){
    super.onPreExecute();
    if(pDialog == null) {
        pDialog = new ProgressDialog(mContext);
        pDialog.setMessage("Loading Playlist...");
        pDialog.show();
    }
    ++pDialogCounter;
}

and change onPostExecute() to:

protected void onPostExecute(Bitmap bitmap) {
    super.onPostExecute(bitmap);
    ...
    if(--pDialogCounter == 0) {
        pDialog.dismiss();
        pDialog = null;
    }
}

This way, you will be creating the dialog only if it didn't exist, and dismissing it only if you have no more AsyncTasks running.

OTHER TIPS

Call the constructor & put pDialog.dismiss(); outside if else

protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
//-----------------
if (bitmap != null) {
    System.out
    .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    mImageView.setImageBitmap(bitmap);
    mHandler.sendEmptyMessage(0);

} else {
    System.out
    .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>NO THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
pDialog.dismiss();
}

Try this :

ProgressDialog dialog; 

protected void onPreExecute() {
          dialog = ProgressDialog.show(importExportActivity.this, "",
                                  "Loading Playlist...", true);
    }

    @Override
    protected void onPostExecute(final Void success) {
          dialog.dismiss();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top