2

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

        });
IssacZH.
  • 1,457
  • 3
  • 24
  • 54

1 Answers1

4

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

EDIT:

Community
  • 1
  • 1
Md Mahbubur Rahman
  • 2,065
  • 1
  • 24
  • 36
  • Thank you for your information. I will try and look into AsyncTask. As you mentioned _AsyncTasks should ideally be used for short operations (a few seconds at the most.)_. For now I wanted to download image so I guess it should be no problem with that. But if later on I want to implement download video feature from the url as well. Is AsyncTask suitable for that task? – IssacZH. Dec 20 '12 at 05:41
  • @IssacZH. Most Welcome as it helped you. :) I have edited my answer as your comment. Please have a look at EDIT portion. – Md Mahbubur Rahman Dec 20 '12 at 05:57
  • Thank you again! Now I know the idea how to progress on this. Your answer is very helpful. – IssacZH. Dec 20 '12 at 06:06
  • @IssacZH. If my answer helped you, then don't forget to accept my answer. Glad to know my answer is very helpful. :) – Md Mahbubur Rahman Dec 20 '12 at 06:08
  • Hi, @Mahbubur R Aaman. I posted out my question regarding about the asyncTask. If it's fine with you, please take a moment to go through with it. Thank you! http://stackoverflow.com/questions/13983517/download-images-with-asynctask – IssacZH. Dec 21 '12 at 02:33
  • @IssacZH. I have go through your question and made an answer. It may help you. :) – Md Mahbubur Rahman Dec 21 '12 at 05:44