1

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);

        }
    }
}
Hayk Nahapetyan
  • 4,452
  • 8
  • 42
  • 61

5 Answers5

8

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);

}
  • how can we give the path if want to resize the image of drawable – swati Sep 27 '12 at 07:54
  • this works for me first you get the original bitmap then you can resize it –  May 15 '13 at 07:15
  • Bitmap bitmapOriginal = BitmapFactory.decodeFile(pathath); image is already load in memory , So i don't think this will help, but be worse. – taotao Jun 02 '15 at 09:55
4

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.

Littm
  • 4,923
  • 4
  • 30
  • 38
  • I tried something like this , but tell the truth it'll not work , but now after your answer I understood it deeply, I'll try again :) thank you very much – Hayk Nahapetyan Sep 03 '12 at 19:11
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();
    }
Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 24
1

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.

Azadi B.
  • 56
  • 3
0

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);
Dulanga
  • 921
  • 11
  • 23