android Image view out of memory error

。_饼干妹妹 提交于 2019-12-20 04:35:13

问题


In my Android project I have imageButton , and after clicking on it , it must open new Activity with imageView , and in my new Activity I must see the ImageButton's image only in large type , my image size is 17mb , and I got out of memory error. But my code works for images with little size. Can somebody help to resize image or change some bitmap options or advice other way? I'm new in android , and sorry for bad English :)

Here is my new Activity's XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:id="@+id/LL11"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
<TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Here must be an image">
</TextView>

<ImageView
    android:id="@+id/imageView1"
    android:maxWidth="10px"
    android:maxHeight="10px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_action_search" />

</LinearLayout>

and the java code

package com.example.example;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;

package com.example.example;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;

public class ActivityTwo extends Activity {


    @Override
      protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog);
        Bundle extras = getIntent().getExtras();
        String path = extras.getString("path");
        if(null != path) 
        {
            Uri myUri = Uri.parse(path);
            super.onCreate(savedInstanceState);
            ImageView img1 = (ImageView) findViewById(R.id.imageView1);
           img1.setImageURI(myUri);

        }
    }
}

回答1:


in your code start at if(null != path) change to this

int size = 10; //minimize  as much as you want
if(path != null){
     Bitmap bitmapOriginal = BitmapFactory.decodeFile(pathath);
     Bitmap bitmapsimplesize = Bitmap.createScaledBitmap(bitmapOriginal,bitmapOriginal.getWidth() / size, bitmapOriginal.getHeight() / size, true);
     bitmapOriginal.recycle();
     img1.setImageBitmap(bitmapsimplesize);

}



回答2:


I have had some problems with Images and OutOfMemory exceptions, and all of them are obviously caused by the fact that I use too much memory that is assigned for the app (called heap).

Like I can see in your code, you create an image every time you push the button, and like you said, if the Image has 17M of size, probably if your device is low quality, it has 20M of max heap for each app, so you are out of memory with two images.

Maybe you can create an image only one time, if you must create more than one image, try to remove previous images, and try to call System.gc(), it could be helpful.

If you really need to create more than one image, more than one time, you can build a reduced instance of image, setting inSampleSize option before you create the image. If you put a value of 2 to this attribute, you will get a 1/2 image of the original, with reduced quality and size.

Something like this:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; // Or other value that you estimate

Then create the image with these options.

PS: It's not necessary to call super.onCreate() more than once.




回答3:


below code can help you for resize an Image

File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                File output = new File(dir, "image.png");


    path = output.getAbsolutePath();
    Bitmap b =  BitmapFactory.decodeFile(cPath);
    Bitmap out = Bitmap.createScaledBitmap(b, 320, 480, false);
    FileOutputStream fout;
    try{
        fout = new FileOutputStream(output);
        out.compress(Bitmap.CompressFormat.PNG, 100, fout);
        fout.flush();
        fout.close();
        b.recycle();
        out.recycle();
    }catch(Exception e){
        e.printStackTrace();
    }



回答4:


I realize this is an old thread, but since I just came across this error today myself, and found that I received the same error, but with a different reason, I wanted to post a different perspective on the matter.

In my case, this was an issue of the dimensions being too large, and not with the size (200K). Upon resizing to a smaller size (640px x 480px) the problem was resolved.

Hope this can help someone else in the future who comes across this post.




回答5:


Above solutions not works in my case, so I found this it work for me Try this one.

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
    BitmapFactory.Options op = new BitmapFactory.Options();
    op.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(
            getActivity().getContentResolver().openInputStream(selectedImage), null, op);

    final int REQUIRED_SIZE = 1000;

    int width_tmp = op.outWidth, height_tmp = op.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options op2 = new BitmapFactory.Options();
    op2.inSampleSize = scale;
    return BitmapFactory.decodeStream(
            getActivity().getContentResolver().openInputStream(selectedImage), null, op2);
}

Then set returned Bitmap in to your imageView like below

yourImageView.setImageBitmap(returnedBitmap);


来源:https://stackoverflow.com/questions/12250300/android-image-view-out-of-memory-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!