Android显示图像位图错误
Posted
技术标签:
【中文标题】Android显示图像位图错误【英文标题】:Android showing image Bitmap Error 【发布时间】:2011-07-04 09:11:59 【问题描述】:您好,我正在尝试从 SD 卡中的所有文件夹中读取图像。所以它创建文件夹列表,如下载、相机等。现在,当我按下任何文件夹时,它会在网格视图中打开照片。我的下载文件夹有小尺寸的图像,应用程序工作正常,但是当我打开具有更大尺寸图像的相机文件夹时,我的应用程序崩溃了。我的grid vie的get view函数如下。
public View getView(int position, View convertView, ViewGroup parent)
ImageView i = new ImageView(mContext);
View v = convertView;
if (v == null)
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.localimageview, null);
BitmapFactory.Options options=new Options();
options.inSampleSize=2;
options.inTempStorage = new byte[16*1024];
Bitmap bb=BitmapFactory.decodeFile(FileList.get(position).toString(),options);
i.setImageBitmap(bb);
i.setLayoutParams(new GridView.LayoutParams(75,75));
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setPadding(8, 8, 8, 8);
v = (View)i;
return v;
它崩溃了 位图 bb=BitmapFactory.decodeFile(FileList.get(position).toString(),options);
07-04 14:21:17.211: ERROR/androidRuntime(11509): FATAL EXCEPTION: main
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at com.ibm.imagegallery.ImageActivity$ImageAdapter.getView(ImageActivity.java:107)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.GridView.makeAndAddView(GridView.java:1219)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.GridView.makeRow(GridView.java:265)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.GridView.fillDown(GridView.java:218)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.GridView.fillFromTop(GridView.java:337)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.GridView.layoutChildren(GridView.java:1111)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.view.View.layout(View.java:7035)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.view.View.layout(View.java:7035)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.view.View.layout(View.java:7035)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.view.View.layout(View.java:7035)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.os.Looper.loop(Looper.java:123)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at java.lang.reflect.Method.invoke(Method.java:521)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-04 14:21:17.211: ERROR/AndroidRuntime(11509): at dalvik.system.NativeStart.main(Native Method)
07-04 14:21:17.221: WARN/ActivityManager(1378): Force finishing activity com.ibm.imagegallery/.ImageActivity
07-04 14:21:17.231: WARN/ActivityManager(1378): Force finishing activity com.ibm.imagegallery/.ImageGallery
【问题讨论】:
此外,如果您发现问题的答案很有用,您应该接受它们的答案(请参阅那里有一个勾号)并使用upvotes。它将帮助您获得更多问题的答案 【参考方案1】:检查例如this question。或者搜索一下,这个是以前回答过的常见问题。
【讨论】:
【参考方案2】:当您访问相机图片时,一旦加载太大而无法处理应用程序,您需要做的是您需要降级图像并将其用于您的活动 可以从这里解决这个问题 Strange out of memory issue while loading an image to a Bitmap object
【讨论】:
【参考方案3】:试试下面的代码。希望有帮助
@Override
public View getView(int position, View convertView, ViewGroup parent)
View row = convertView;
ViewWrapper wrapper = null;
try
if (row == null)
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.gallery_template, parent,
false);
wrapper = new ViewWrapper(row);
row.setTag(wrapper);
else
wrapper = (ViewWrapper) row.getTag();
Bitmap bitmap = getImage(position);
if (bitmap != null )
wrapper.getIcon().setImageBitmap(bitmap);
else
showErrorDialogue(getString(R.string.noGalleryData));
catch (Exception e)
return (row);
private Bitmap getImage(int position)
Bitmap bitmap = null;
if (Constants.IMAGES_PATH.size() > 0)
try
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
String path = Constants.IMAGES_PATH.get(position);
// -------- Decode THE BITMAP AND RESIZE IT
bitmap = BitmapFactory
.decodeFile(path,options));
catch (Exception e)
return bitmap;
【讨论】:
以上是关于Android显示图像位图错误的主要内容,如果未能解决你的问题,请参考以下文章