2013-03-19 29 views
0

我试图从服务器下载图像并将其保存在android的内部存储中。如果我下载的图像小于2 MB,它在任何情况下都能正常运行。但是当我下载大小超过2MB的JPEG图像时,我面临两个问题。与Android下载图像相关的问题

第一个问题是,当我试图使用模拟器从服务器下载并保存映像时,出现“位图大小超出虚拟机预算”和活动崩溃等错误。

第二个问题是,当我在手机上运行应用程序时,这个问题不是他们的问题,而是另一个问题正在发生。当我显示下载的图像时,图像还包含一些其他颜色(彩虹种类)。

这里是我使用它来下载并保存图像的代码:

//For downloading the image file 
    void downloadFile(String fileUrl) 
    { 
     URL myFileUrl = null; 
     try { 
      myFileUrl = new URL(fileUrl); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try 
     { 
      HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection(); 
      conn.setDoInput(true); 
      conn.connect(); 
      InputStream is = conn.getInputStream(); 
      bmImg = BitmapFactory.decodeStream(is); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    //For saving the downloaded image 
    void saveImage() { 
     try 
     { 
      String fileName ="displayimg.png"; 
      final FileOutputStream fos = openFileOutput(fileName, Activation.MODE_PRIVATE); 
      bmImg.compress(CompressFormat.PNG, 90, fos); 

     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

    } 

这里是显示图像的代码:

private Bitmap myBitmap; 
myBitmap= BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
ivDisplayImage.setImageBitmap(myBitmap); 
+0

为什么你不保存你下载的内容,而不是解压缩图像并以另一种格式再次压缩它? – njzk2 2013-03-19 08:37:11

+1

@ njzk2他会在ImageView中显示它,所以它不会帮助解决原始问题。 – vorrtex 2013-03-19 08:44:27

+1

对于第一个问题,我会建议你回收你的位图images.as立即离开包含图库图像的课程 – Aamirkhan 2013-03-19 08:50:22

回答

0

嘿,我会建议你使用这个库:http://loopj.com/android-async-http/

下面是示例代码:

  AsyncHttpClient client = new AsyncHttpClient(); 
     String[] allowedContentTypes = new String[] { "image/jpeg" }; // here you need to type the mimetype of the file 

     client.get("link to jpg or any image"), 
       new BinaryHttpResponseHandler(allowedContentTypes) { 
        @Override 
        public void onSuccess(byte[] fileData) { 
         // this means it downloaded succesfully and you could make a filoutputstream to sdcard 
         FileOutputStream fos; 
         try { 
          fos = new FileOutputStream(Environment 
            .getExternalStorageDirectory() 
            + "/Images/test.jpg"); 

          fos.write(fileData); 
          fos.close(); 
          fos.flush(); 
         } catch (FileNotFoundException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } finally { 
         // here you could set it to an imageview or do anything you wouyld like 

         } 
        } 
       }); 
    } 

如果您有任何问题,请告诉我。

+0

我认为不需要将图像存储在2mb字节[]如你的解决方案所示。它可以直接提交到存储。 – njzk2 2013-03-19 08:38:45

2

您必须使用ImageLoader class,它会缩放位图并在imageview中显示它。

也可以按照this tutorial从网页显示图像。

希望这可以帮助你。

3

你的代码有一些问题,第一件事是你试图用PNG格式压缩图像,PNG格式不压缩。你应该insted使用jpeg。这将减少图像尺寸和质量。其次,有一种非常有效的方式来下载图像并在imageview中显示,这里是链接:Displaying Bitmaps Efficiently它也解释了你的第一个问题java.lang.OutofMemoryError:位图大小超过虚拟机预算。 也有一些好的库,我建议你使用的主题像

+0

为什么PNG格式不压缩? – Raghunandan 2013-03-19 08:51:39

+0

@Raghunandan Long back我尝试使用这种方法压缩PNG文件public boolean compress(Bitmap.CompressFormat格式,int质量,OutputStream流)但它没有工作,然后我做了一些搜索,发现这个http://developer.android。 com/reference/android/graphics/Bitmap.html质量 - 提示压缩机,0-100。 0表示小尺寸压缩,100表示​​最大质量压缩。某些格式(如无损PNG)会忽略质量设置。我还发现一些问题,请点击此处。 https://code.google.com/p/android/issues/detail?id=2092 – 2013-03-19 09:30:26

+0

是图像显示原因没有原始颜色? – Raghunandan 2013-03-19 09:36:05

1

您可以使用通用图像装载机。 https://github.com/nostra13/Android-Universal-Image-Loader。异步下载图像。将图像缓存在内存或SD卡中。

你也可以看看懒惰加载https://github.com/thest1/LazyList

还可以在不使用时回收位图。

bitmap.recycle(); 

要高效加载bitmpas,请参阅链接中的文档。 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

java.lang.OutofMemoryError:位图大小超过VM预算意味着您有内存泄漏。 http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

http://www.youtube.com/watch?v=_CruQY55HOk。这次谈话是关于内存管理和避免内存泄漏。还解释了如何使用MAT分析器来查找内存泄漏。

Bitmap color change while compressing to png。检查可能与您的问题有关的答案。同时检查您的手机正在运行的Android版本。

Bitmap color change while compressing to png。检查这个链接。