Jack
Jack

Reputation: 1855

Out of Memory Error ,set bitmap on TouchImageview

In my app i am using a Touch imageview to enable zoom and crop option for user.For that first i have converted the image uri to bitmap using this method .

Uri to bitmap

   private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 140;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    String imageType = o.outMimeType;
    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;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);

}
public Bitmap decodeFile(String filePath) {
    //Log.e(TAG, "Camera Image 3");
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    BitmapFactory.decodeFile(filePath, o);
    final int REQUIRED_SIZE = 1024;
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    o.inJustDecodeBounds = false;

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
  Bitmap  bitmap = BitmapFactory.decodeFile(filePath, o2);
    //  imageView = (ImageView) findViewById(R.id.imgView);
    // imageView.setImageBitmap(bitmap);
    return bitmap;
}

After that i have Scaled my bitmap using this method

   public Bitmap scaleBitmap(Bitmap rotated)
{

    final int maxSize = 300;
    int outWidth;
    int outHeight;
    int inWidth = rotated.getWidth();
    int inHeight = rotated.getHeight();
    if(inWidth > inHeight){
        outWidth = maxSize;
        outHeight = (inHeight * maxSize) / inWidth;
    } else {
        outHeight = maxSize;
        outWidth = (inWidth * maxSize) / inHeight;
    }

    Bitmap resizedBitmap = Bitmap.createScaledBitmap(rotated, outWidth, outHeight, false);


    // imageView = (ImageView) findViewById(R.id.imgView);
    // imageView.setImageBitmap(resizedBitmap);

   // mGPUImageView = (GPUImageView) findViewById(R.id.gpuimage);
   // mGPUImageView.setImage(resizedBitmap);
    return resizedBitmap;

}

The output of this method that is a resizedBitmap ,I have set this bitmap to imageview like this

   touchimageview.setImageBitmap( scaleBitmap(decoded));

After doing all these scaling and decoding techniques still i am getting out of memeory error ,How can i solve this issue can anyone help ??

Upvotes: 1

Views: 446

Answers (2)

Tom
Tom

Reputation: 296

Try to load Image to TouchIamgeView using UIL displayImage function.

Try this;

ImageLoader imageloader;
 TouchImageView touchImageView = (TouchImageView)findViewById(R.id.);


    imageLoader.displayImage(imageURL,touchImageView);

Upvotes: 1

johni07
johni07

Reputation: 771

As a quick fix you can try to add this to your Application tag in the Manifest file:

android:largeHeap="true"

BUT:

Never request a large heap simply because you've run out of memory and you need a quick fix—you should use it only when you know exactly where all your memory is being allocated and why it must be retained.

More information: Android Dev - Managing app memory

Upvotes: -1

Related Questions