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

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

问题描述

在我的应用程序,系统会从URL下载图像,并保存到手机内存。的(我没有包括在问题URL地址)的最重要的是,它也将数据保存到SQLite数据库。数据保存为文件名,文件路径和文件大小。但目前我一旦经过下载过程是否在下载完成或失败的过程中的中间,它也将插入到数据库中。结果,
有没有办法,我可以查看下载过程是否完成没有什么办法?

  GalleryScreen.this.runOnUiThread(新的Runnable(){
            @覆盖
            公共无效的run(){
                布尔isDownloadResult = FALSE;
                        INT NumIncrease = 0;
                        Log.i(TAG,NumberIncrease:+ NumIncrease);
                        Toast.makeText(getApplicationContext(),下载.............>>>>>>>>>>>中,Toast.LENGTH_SHORT )。显示();
                        位图BM;
                        在的InputStream;                        尝试{
                            在=新的java.net.URL(URL).openStream();
                            BM = BitmapFactory.de codeStream(新PatchInputStream(中));                            文件存储=新的文件(Environment.getExternalStorageDirectory()+文件分割符+/图片/);
                            Log.i(TAG,存储+存储);
                            Log.i(TAG,存储+ storage.getAbsolutePath());
                            如果(!storage.exists()){
                                storage.mkdirs();
                            }                                字符串文件名=/\"+CONTENT_ID+\".jpg;
                                FOS的FileOutputStream =新的FileOutputStream(存储+文件名);
                                bm.com preSS(Bitmap.Com pressFormat.JPEG,85,FOS);                                字符串的文件路径=存储+文件名;
                                文件fil​​echeck =新的文件(文件路径);
                                长档案大小= filecheck.length();
                                fos.flush();
                                fos.close();                                Log.i(TAG,BM:+ BM);
                                Log.i(TAG,FOS:+ FOS);
                                Log.i(TAG,文件大小:+档案大小);
                                Log.i(TAG,文件路径:+文件路径);
                                helper.insert_content(文件路径,档案大小,requestTime);
                            isDownload = FALSE;
                        }
                        赶上(IOException异常E1){
                                e1.printStackTrace();
                                }
            }        });


解决方案

请使用AsyncTask的。 AsyncTask的正确实现和易于使用的用户界面线程。这个类允许无需操作线程和/或处理程序进行后台操作并发布在UI线程上的结果。

的AsyncTask的设计是围绕主题和Handler一个辅助类,并不构成通用线程框架。 AsyncTasks应理想(至多几秒钟。)用于短操作

你可以使用匿名类异步任务。这将是这样的:

  ImageView的mChart =(ImageView的)findViewById(R.id.imageview);
字符串的URL =HTTP://www...anything ......;mChart.setTag(URL);
新DownloadImageTask.execute(mChart);

Task类:

 公共类DownloadImagesTask扩展的AsyncTask< ImageView的,无效的,位图> {ImageView的ImageView的= NULL;@覆盖
保护位图doInBackground(ImageView的... imageViews){
    this.imageView = imageViews [0];
    返回download_Image((字符串)imageView.getTag());
}@覆盖
保护无效onPostExecute(位图结果){
    imageView.setImageBitmap(结果);
}
私人位图download_Image(字符串URL){
   ...
}

在这里, onPostExecute ,您可以轻松地查询下载图像的过程是否完成与否。

进一步阅读

编辑:


  • 如果您要下载较大的文件,你可能会考虑
    把你的应用程序到某种类型的服务的,因为这会
    可能需要几个小时。

  • 您可以考虑使用下载管理器的新设备
    (与Android 2.3 +)

  • 也是一个很好的资源 - >
    <一href=\"http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog\">Download在ProgressDialog
  • 与Android的文件,并显示进度

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

        });

解决方案

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:

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

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