2011-08-08 56 views
1

我使用BitmapFactory解碼文件中的JPG,然後將與imageBitmap一起使用。有時,應用程序崩潰與「java.lang.OutOfMemoryError: bitmap size exceeds VM budgetjava.lang.OutOfMemoryError:位圖大小超過虛擬機預算

我已經嘗試使用:

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 2; 
Bitmap bm = BitmapFactory.decodeFile(filepath, options); 

它似乎是工作了一段時間,但現在不是了。我能做什麼?

+0

什麼是圖像分辨率? – MobileCushion

回答

1

在Android中,有多少內存,可以分配給應用程序一個帽,它可能是大約16 MB到32 MB。 (高達的Android版)

對於爲位圖所需的200萬像素,存儲器將是2 * 4 = 8 M.

爲了減少內存消耗,需要降低圖像的分辨率。說WVGA,800x480的開始與

詞shash

+0

所以現在,我必須設置爲setJpegQuality(100)的Camera.Parameters。我想我需要改變它? – LuxuryMode

+1

調整圖像分辨率 – Shash316

+0

感謝。我怎麼做? – LuxuryMode

0

減少所使用的圖像的數量,或將它們緩存,如果緩存它們,將它們存儲在您需要的最小尺寸。

問候, 斯特凡

+0

如何將它們存儲在特定的大小? – LuxuryMode

+0

這種方式來調整它們的大小:http://www.anddev.org/resize_and_rotate_image_-_example-t621.html – Snicolas

0

這是緩存圖像文件的代碼。

protected static Map<String, Drawable> cacheThumbs = new HashMap<String, Drawable>(); 

//代碼緩存圖片

   if(thumbUri.length() == 0) 
        return; 
       Drawable d = cacheThumbs.get(thumbUri); 
       if(d == null) 
       { 
        String fileName = movie.getId()+"_"+thumbUri.substring(thumbUri.lastIndexOf('/')+1); 
        cacheFile = new File(application.getCacheDir(), fileName); 
        if(!cacheFile.exists()) 
        { 
         //Log.v(LOG_TAG,"Fetching "+thumbUri +" and caching in "+cacheFile.getAbsolutePath()); 
         InputStream bis = (InputStream) new URL(thumbUri).getContent(); 
         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(cacheFile)); 
         int read = 0; 
         while((read = bis.read(buffer)) != -1) 
          bos.write(buffer, 0, read); 
         bis.close(); 
         bos.close();       
         Log.v(LOG_TAG,thumbUri + " fetched"); 
        }//if 
        Log.v(LOG_TAG,thumbUri + " in cache"); 
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(cacheFile), 9000); 
        d = Drawable.createFromStream(bis, "src name"); 
        bis.close(); 
        cacheThumbs.put(thumbUri, d); 

問候, 斯特凡

0

我想不同的看法加入討論。我確實閱讀了很多線索和建議,最後用WebView顯示單個圖像。這不僅僅是爲了一幅圖像畫廊。它的工作完美,它的可縮放的選項,它的滾動,最後沒有內存錯誤;-)

你的作品從網上加載圖像和局部影像。這裏有個小例子:

WebView webView = (WebView) findViewById(R.id.your_webview); 

String html = "<html><head><title>A title</title></head><body bgcolor=\"#000000\"><img src=\"" + url + "\"></body></html>"; 

WebSettings webSettings = webView.getSettings(); 
webSettings.setBuiltInZoomControls(true); 
webSettings.setLoadWithOverviewMode(true); 
webSettings.setSupportZoom(true); 
webSettings.setUseWideViewPort(true); 

webView.setScrollbarFadingEnabled(true); 
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
webView.loadDataWithBaseURL("fake://fake.com", html, "text/html", "UTF-8", null); 
相關問題