异步任务中的进度对话框(PostExecute)

时间:2014-02-24 13:18:01

标签: android android-listview android-asynctask android-bitmap

我已经实现了一个异步任务,可以为视频播放列表创建缩略图。在后台线程中,我实现了一个在PreExecute Task中触发的Progress对话框。目前我正在解雇postExecute方法上的对话但我必须遗漏一些东西。我能帮忙吗?

我的代码在

下面

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


}

3 个答案:

答案 0 :(得分:1)

调用构造函数&amp;将pDialog.dismiss();放在外面,否则

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

答案 1 :(得分:1)

问题是你一遍又一遍地创建新的ProgressDialogs,并且丢失对旧的ProgressDialogs的引用。

由于我没有在pDialog内的任何地方看到AsyncTask,我猜它是外部的。另一方面,您显然正在创建多个AsyncTasks来处理多个图像。这意味着您运行了多次代码:

pDialog = new ProgressDialog(mContext);

每次都丢失旧的ProgressDialog。


建议的解决方案:

添加一个计数器,以及pDialog:

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

然后将onPreExecute()更改为:

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

并将onPostExecute()更改为:

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

这样,只有当对话框不存在时才会创建对话框,只有在没有AsyncTasks运行的情况下才会解除对话框。

答案 2 :(得分:0)

试试这个:

ProgressDialog dialog; 

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

    @Override
    protected void onPostExecute(final Void success) {
          dialog.dismiss();
    }