无法保存来自URL的图像。安卓

安东尼奥314

首先,对我的英语不好对不起,其次,我有一个“小”问题。我测试了很多StackOverFlow的代码,但我仍然遇到相同的问题。

我正在尝试从URL下载一些图像。我有一个ExpandableListView,并且使用一个名为Downloadusers的类来下载有关用户的所有信息。在此类中,我获取用户的照片URL,并使用以下代码下载图像:

 private void downloadFile(String url) {

    String filepath = null;
    try
    {   
      URL nurl = new URL(url);
      HttpURLConnection urlConnection = (HttpURLConnection) nurl.openConnection();
      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(true);                   
      urlConnection.connect();                  
      File SDCardRoot = getExternalFilesDir(null);
      String filename = url.substring(url.lastIndexOf('/') + 1);  
      Log.i("Local filename:",""+filename+"  SDCardRoot: "+SDCardRoot.toString());
      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[PHOTO_FILE_MAX_SIZE];
      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.i("filepath:"," "+filepath);
}

我还验证了URL正确,并且已将其加载到浏览器中。使用Log.i(“ filepath:”,“” + filepath); 我可以看到文件路径是正确的,所以我认为图像下载正确,但是没有,图像文件是损坏的文件,因此当我将图像加载到ImageView中时,我遇到了NullPointException,因为读取的bMap由于图像损坏而为null。

我拥有所有权限:Internet,写入和读取外部存储以及电话状态。

我也尝试使用AsyncTask下载图像,但是我有同样的问题。

有人知道我的问题是什么?谢谢。

安东尼奥314

我使用库nostra13“ universal-image-loader-1.9.2.jar”解决了我的问题我的代码现在是:

    // If the user photo exists and is public, download and show it. 
    if (Utils.connectionAvailable(activity)
            && (photoFileName != null) && !photoFileName.equals("")
            && !photoFileName.equals(Constants.NULL_VALUE)) {

        // Create options. Setting caché = true (default = false)
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                            .cacheInMemory(true)
                            .build();

         // Create global configuration and initialize ImageLoader 
         // with this configuration
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                                 .Builder(activity.getApplicationContext()) 
                                 .defaultDisplayImageOptions(options)
                                 .build();

        ImageLoader.getInstance().init(config);

        // Load image, decode it to Bitmap and display Bitmap in ImageView 
        // (or any other view 
        // which implements ImageAware interface)
        ImageLoader.getInstance().displayImage(photoFileName, image);
   }

使用该代码,我可以将图像加载到caché上,并在imageview中显示,而不会出现问题。

谢谢大家。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章