Android java.lang.OutOfMemoryError:位图大小超出VM预算

Android java.lang.OutOfMemoryError:位图大小超出VM预算,android,Android,我正在使用BitmapFactory对文件中的JPG进行解码,然后用它设置ImageView的imageBitmap。有时,应用程序会因“java.lang.OutOfMemoryError:位图大小超过VM预算”而崩溃 我试过使用: BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap bm = BitmapFactory.decodeFile(filepath

我正在使用
BitmapFactory
对文件中的JPG进行解码,然后用它设置
ImageView
imageBitmap
。有时,应用程序会因“
java.lang.OutOfMemoryError:位图大小超过VM预算”而崩溃

我试过使用:

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

它似乎工作了一段时间,但不再工作了。我能做什么?

减少您使用的图像数量,或者缓存它们,如果您缓存它们,请将它们存储在您需要的最小大小

问候,,
Stéphane

减少您使用的图像数量,或者缓存它们,如果您缓存它们,请将它们存储在您需要的最小大小

问候,,
Stéphane

在Android中,可以分配给应用程序的内存有一个上限,大约是16MB到32MB。(最高达Android版本)

对于200万像素,位图所需的内存为2*4=8 M

要减少内存消耗,需要降低图像分辨率。先说WVGA,800x480


Shash

在安卓系统中,可以分配给应用程序的内存有一个上限,大约是16MB到32MB。(最高达Android版本)

对于200万像素,位图所需的内存为2*4=8 M

要减少内存消耗,需要降低图像分辨率。先说WVGA,800x480


Shash

这是缓存图像文件的代码

protected static Map<String, Drawable> cacheThumbs = new HashMap<String, Drawable>();
问候,,
这是用来缓存图像文件的代码

protected static Map<String, Drawable> cacheThumbs = new HashMap<String, Drawable>();
问候,,
斯泰芬尼

我想为这次讨论添加一种不同的观点。我读了这么多的帖子和建议,最终得到了一个显示单个图像的WebView。这不是画廊的展览,只是为了一张图片。它工作完美,可按选项缩放,可滚动,最终没有内存错误;-)

它适用于从web加载的图像以及本地图像。下面是一个小例子:

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);
WebView WebView=(WebView)findViewById(R.id.your_WebView);
字符串html=“一个标题”;
WebSettings WebSettings=webView.getSettings();
webSettings.SetBuilTinZoomControl(真);
webSettings.SetLoadWithOverview模式(true);
webSettings.setSupportZoom(真);
webSettings.setUseWideViewPort(true);
webView.SetScrollBarFadinEnabled(真);
webView.setScrollBarStyle(webView.SCROLLBARS\u外部\u覆盖);
webView.loadDataWithBaseURL(“fake://fake.com,html,“text/html”,“UTF-8”,null);

我想为这次讨论添加一个不同的观点。我读了这么多的帖子和建议,最终得到了一个显示单个图像的WebView。这不是画廊的展览,只是为了一张图片。它工作完美,可按选项缩放,可滚动,最终没有内存错误;-)

它适用于从web加载的图像以及本地图像。下面是一个小例子:

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);
WebView WebView=(WebView)findViewById(R.id.your_WebView);
字符串html=“一个标题”;
WebSettings WebSettings=webView.getSettings();
webSettings.SetBuilTinZoomControl(真);
webSettings.SetLoadWithOverview模式(true);
webSettings.setSupportZoom(真);
webSettings.setUseWideViewPort(true);
webView.SetScrollBarFadinEnabled(真);
webView.setScrollBarStyle(webView.SCROLLBARS\u外部\u覆盖);
webView.loadDataWithBaseURL(“fake://fake.com,html,“text/html”,“UTF-8”,null);

图像分辨率是多少?图像分辨率是多少?现在,我将相机参数设置为setJpegQuality(100)。我想我需要改变它?所以现在,我把相机的参数设置为setJpegQuality(100)。我想我需要更改它?我如何将它们存储为特定大小?这种方法可以调整它们的大小:我如何将它们存储为特定大小?这种方法可以调整它们的大小: