How to check whether the process of download image is completed or not?

情到浓时终转凉″ 提交于 2019-12-13 02:19:24

问题


In my application the system will download images from an url and save it into phone memory. (I did not included url address in the question) On top of that, it will also save data into sqlite database. Data that save is file name, filepath and file size. But currently once I go through the download process whether the download complete or fail in the middle of the process, it also will insert into the database.
Is there any way that I can check whether the download process is completed or not?

        GalleryScreen.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                boolean isDownloadResult = false;


                        int NumIncrease = 0;
                        Log.i(TAG, "NumberIncrease:" +NumIncrease);
                        Toast.makeText(getApplicationContext(), "Downloading.............>>>>>>>>>>>", Toast.LENGTH_SHORT).show();
                        Bitmap bm;
                        InputStream in;



                        try{


                            in = new java.net.URL(URL).openStream();
                            bm = BitmapFactory.decodeStream(new PatchInputStream(in));

                            File storage = new File(Environment.getExternalStorageDirectory() + File.separator + "/Images/");
                            Log.i(TAG,"storage:" +storage);
                            Log.i(TAG,"storage:" +storage.getAbsolutePath());
                            if(!storage.exists()){
                                storage.mkdirs();


                            }

                                String FileName = "/"+CONTENT_ID+".jpg"; 
                                FileOutputStream fos = new FileOutputStream(storage + FileName);
                                bm.compress(Bitmap.CompressFormat.JPEG, 85, fos);

                                String filepath = storage + FileName;
                                File filecheck = new File (filepath);
                                long fileSize = filecheck.length();
                                fos.flush();
                                fos.close();

                                Log.i(TAG, "bm:" +bm);
                                Log.i(TAG, "fos:" +fos);
                                Log.i(TAG, "filesize:" +fileSize);
                                Log.i(TAG, "filepath:" +filepath);
                                helper.insert_content(filepath, fileSize, requestTime);


                            isDownload = false;
                        }
                        catch(IOException e1){
                                e1.printStackTrace();
                                }   
            }

        });

回答1:


Please use AsyncTask. AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.)

you can just use an anonymous class for the async task. This would like this:

ImageView mChart = (ImageView) findViewById(R.id.imageview);
String URL = "http://www...anything ...";

mChart.setTag(URL);
new DownloadImageTask.execute(mChart);

The Task class:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

ImageView imageView = null;

@Override
protected Bitmap doInBackground(ImageView... imageViews) {
    this.imageView = imageViews[0];
    return download_Image((String)imageView.getTag());
}

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


private Bitmap download_Image(String url) {
   ...
}

Here, onPostExecute, you can easily check whether the process of download image is completed or not.

Further reading

  • http://developer.android.com/reference/android/os/AsyncTask.html
  • Loading Image using AsyncTask
  • Android : Loading an image from the Web with Asynctask
  • Android Help with adding a progress dialog while image loading?

EDIT:

  • If you are trying to download larger files, you might consider putting your application into some type of Service as this would potentially take a few hours.
  • You can consider using Download Manger for newer devices (with Android 2.3+)
  • Also a nice resource -> Download a file with Android, and showing the progress in a ProgressDialog


来源:https://stackoverflow.com/questions/13965294/how-to-check-whether-the-process-of-download-image-is-completed-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!