Android 显示图像位图错误
您好,我正在尝试从 SD 卡中的所有文件夹中读取图像。因此,它会创建文件夹列表,例如下载、相机等。现在,当我按任何文件夹时,它会在网格视图中打开照片。我的下载文件夹包含小尺寸图像,应用程序工作正常,但是当我打开包含较大尺寸图像的相机文件夹时,我的应用程序崩溃了。我的网格视图获取功能如下。
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
Hi I am trying to read images from all the folders in SD card. So its create folder list like download, camera etc. Now when I press any of the folder it open the Photos in the Grid view. My download folders has images of small size and the application works fine but when i open the camera folder which has bigger size images my application is crashing. My get view function of grid vies is as follows.
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;
}
}
}
Its crashing at
Bitmap 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查例如这个问题。或者搜索一下,这是以前回答过的常见问题。
Check for example this question. Or do a search, this is a common problem answered before.
当您访问相机图片时,一旦加载的图片太大,应用程序无法处理,您需要做的是降低图像质量并将其添加到您的活动中
这个问题的解决方案可以从这里找到
将图像加载到 Bitmap 对象时出现奇怪的内存不足问题
When You access the camera pictures the once which get loaded are too large to handle for the application what you need to do is that you need to degrade the images and get it on your activity
a solution to this problem can be taken from here
Strange out of memory issue while loading an image to a Bitmap object
尝试以下代码。希望有帮助
Try following code.Hope this help