如何检查下载图像的进程是否完成? [英] How to check whether the process of download image is completed or not?

查看:114
本文介绍了如何检查下载图像的进程是否完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,系统将从URL下载图像并将其保存到手机内存中。 (我没有在问题中包含url地址)此外,它还将数据保存到sqlite数据库中。保存的数据是文件名,文件路径和文件大小。但是,目前一旦我完成了下载过程,无论下载完成还是在进程的中间失败,它也会插入数据库。

有什么办法可以检查下载过程是否是否完成?

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

        });


推荐答案

请使用AsyncTask。 AsyncTask可以正确轻松地使用UI线程。该类允许执行后台操作并在UI线程上发布结果,而不必操纵线程和/或处理程序。

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被设计为围绕Thread和处理程序并不构成通用的线程框架。理想情况下,AsyncTasks应用于短操作(最多几秒钟)。

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

Task类:

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) {
   ...
}

这里, onPostExecute ,您可以轻松查看下载图像的进程是否完成。

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

进一步阅读

  • 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?

编辑:

  • 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

这篇关于如何检查下载图像的进程是否完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆