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
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.
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);
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);
}
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();
}
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.