OutOfMemory Exception when handling images [duplicate]
Possible Duplicate:
OutOfMemoryError: bitmap size exceeds VM budget :- Android
Im in the process of writing a program which uses images from the gallery and then displays them in an activity (one image pr. activity). However I've been bumping into this error over and over again for three days straight without doing any progress of eliminating it:
07-25 11:43:36.197: ERROR/AndroidRuntime(346): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
My code flow is as follows:
When the user presses a button an intent is fired leading to the gallery:
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 0);
Once the user has selected an image the image is presented in an imageview:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:background="#ffffffff"
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:maxWidth="250dip"
android:maxHeight="250dip"
android:adjustViewBounds="true"/>
</LinearLayout>
In the onActivityResult method i have:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case 0: // Gallery
String realPath = getRealPathFromURI(data.getData());
File imgFile = new File(realPath);
Bitmap myBitmap;
try {
myBitmap = decodeFile(imgFile);
Bitmap rotatedBitmap = resolveOrientation(myBitmap);
img.setImageBitmap(rotatedBitmap);
OPTIONS_TYPE = 1;
} catch (IOException e) { e.printStackTrace(); }
insertIma开发者_Go百科geInDB(realPath);
break;
case 1: // Camera
The decodeFile method is from here and the resolveOrientation method just wraps the bitmap into a matrix and turns it 90 degrees clockwise.
I really hope someone can help me resolve this matter.
it is because your bitmap size is large, so reduce the image size manually, or by programmatically
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap = BitmapFactory.decodeFile(mPathName, options);
Your GC does not run. Try getting your Bitmap by pieces
BitmapFactory.Options buffer = new BitmapFactory.Options();
buffer.inSampleSize = 4;
Bitmap bmp = BitmapFactory.decodeFile(path, buffer);
There are many question in Stackoverflow about bitmap size exceeds VM budget so first search about your issue and when you cant find any solution then ask question here
The problem is because your bitmap's size is too large than the VM can handle. For example from your code I can see that you are trying to paste an Image into imageView which you are capturing using Camera. So normally the camera images will be too large in size which will rise this error obviously. So as others have suggested, you have to compress your image either by sampling it or convert your image into smaller resolution. For example if your imageView is 100x100 in width and height, you can create a scaled bitmap so that your imageView gets filled exactly. You can do this for that,
Bitmap newImage = Bitmap.createScaledBitmap(bm, 350, 300,true);
or you can sample it in methods what user hotveryspicy have suggested.
精彩评论