java - 如何将Android图片下载器修改为AsyncTask

标签 java android performance android-asynctask android-download-manager

有人可以帮我吗?如何使用 AsyncTask 而不是 Try block 重新使用下面的示例。

因为我只想从 Web 下载图像保存到 SD 并获取返回的存储路径,所以我可以将其加载到 imageView 上。

我正在使用一个库https://github.com/Bearded-Hen/Android-Bootstrap我有圆形缩略图 ImageView。我只能使用抽屉图像来更改此 View 的图像。这就是为什么我尝试先保存图像然后加载到这个自定义 imageView

String filepath = "";
                    try
                    {   
                      URL url = new URL("http://d3sdoylwcs36el.cloudfront.net/VEN-virtual-enterprise-network-business-opportunities-small-fish_id799929_size485.jpg");
                      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                      urlConnection.setRequestMethod("GET");
                      urlConnection.setDoOutput(true);                   
                      urlConnection.connect();                  
                      File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
                      String filename="downloadedFile.png";   
                      Log.i("Local filename:",""+filename);
                      File file = new File(SDCardRoot,filename);
                      if(file.createNewFile())
                      {
                        file.createNewFile();
                      }                 
                      FileOutputStream fileOutput = new FileOutputStream(file);
                      InputStream inputStream = urlConnection.getInputStream();
                      int totalSize = urlConnection.getContentLength();
                      int downloadedSize = 0;   
                      byte[] buffer = new byte[1024];
                      int bufferLength = 0;
                      while ( (bufferLength = inputStream.read(buffer)) > 0 ) 
                      {                 
                        fileOutput.write(buffer, 0, bufferLength);                  
                        downloadedSize += bufferLength;                 
                        Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
                      }             
                      fileOutput.close();
                      if(downloadedSize==totalSize) filepath=file.getPath();    
                    } 
                    catch (MalformedURLException e) 
                    {
                      e.printStackTrace();
                    } 
                    catch (IOException e)
                    {
                      filepath=null;
                      e.printStackTrace();
                    }
                    Log.e(TAG, "''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''");
                    Log.i("filepath:"," "+filepath) ;
                    //return filepath;

我得到文件路径的 NULL 值

最佳答案

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

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

任务类:

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

关于java - 如何将Android图片下载器修改为AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27795640/

相关文章:

java - 使用 Equal 方法时遇到问题

java - Font.createFont 将文件留在临时目录中

r - 什么时候值得在 R 函数中使用 `remove`?

mysql 2.5M 行计数慢

java - android Activity 不会显示

performance - 折叠优化

java - 单点登录框架

java - 计划没有给我想要的结果

android - 本地存储被 Cordova 和 admob 覆盖

java - 水平 EditText 一次滚动一个字符