2011-06-13 17 views
5

我試圖下載一個1MB圖像文件,然後保存到位圖來填充imageview。 但顯示錯誤android如何下載一個1MB圖像文件並設置爲ImageView

06-13 13:39:48.149: ERROR/AndroidRuntime(1782): 
java.lang.OutOfMemoryError: bitmap size exceeds VM budget 

如何下載大圖像?

+0

重複:http://stackoverflow.com/questions/1949066/java- lang-outofmemoryerror-bitmap-size-exceed-vm-budget-android – razlebe 2011-06-13 09:41:47

回答

3

我曾經經歷過同樣的問題。在將下載的位圖設置爲您的ImageView之前,您必須根據您的ImageView的寬度和高度來壓縮位圖。

你必須創建一個縮放位圖這個樣子,

bm1=Bitmap.createScaledBitmap(bm, 300, 300,true); 
imgView.setImageBitmap(bm1); 

或壓縮位圖這個樣子,

  OutputStream fOut = null; 
         File file = new File(strDirectoy,imgname); 
          fOut = new FileOutputStream(file); 

         bm1.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
          fOut.flush(); 
          fOut.close(); 

       MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()); 
+0

bm1 = Bitmap.createScaledBitmap(bm,300,300,true); imgView.setImageBitmap(bm1); 有時它工作,有時它不工作。你能告訴我爲什麼嗎? – Jyosna 2011-06-16 12:29:05

+0

如果不嘗試我給出的下一個方法。這將壓縮並存儲您的圖像。之後,嘗試使用uri爲imageview設置圖像。 – 2011-06-16 12:41:25

相關問題