0

I am trying to load an image file from my gallery, but am getting null instead of the expected image. Here is the code I am using:

public static Bitmap decodeSampledBitmapFromStreem(InputStream is,
        int reqWidth, int reqHeight) {
    Bitmap b = null;
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    options.inJustDecodeBounds = false;
    b = BitmapFactory.decodeStream(is, null, options);
    return b;
}
public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

Can you see any problem in my code? Why am I getting null?

Please note that my code works fine for small images if i comment the 6th line, however I am getting error for larger images as per my error Log:

05-17 11:37:46.277: E/AndroidRuntime(9224): FATAL EXCEPTION: AsyncTask #3
05-17 11:37:46.277: E/AndroidRuntime(9224): java.lang.RuntimeException: An error occured while executing doInBackground()
05-17 11:37:46.277: E/AndroidRuntime(9224):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.lang.Thread.run(Thread.java:1019)
05-17 11:37:46.277: E/AndroidRuntime(9224): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-17 11:37:46.277: E/AndroidRuntime(9224):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at com.giftcard.GiftCard.decodeSampledBitmapFromStreem(GiftCard.java:454)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at com.giftcard.GiftCard$MyBitmapDecoder.doInBackground(GiftCard.java:500)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at com.giftcard.GiftCard$MyBitmapDecoder.doInBackground(GiftCard.java:1)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
05-17 11:37:46.277: E/AndroidRuntime(9224):     ... 4 more
05-17 11:37:49.387: I/Process(9224): Sending signal. PID: 9224 SIG: 9
thejartender
  • 9,339
  • 6
  • 34
  • 51
user1395885
  • 56
  • 1
  • 6

4 Answers4

1

it can be due to multiple use of isnputStream try avoiding first use of BitmapFactory.decodeStream(is, null, options);

public static Bitmap decodeSampledBitmapFromStreem(InputStream is,
        int reqWidth, int reqHeight) {
    Bitmap b = null;
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    **BitmapFactory.decodeStream(is, null, options);**
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    options.inJustDecodeBounds = false;
    b = BitmapFactory.decodeStream(is, null, options);
    return b;
}
dinesh sharma
  • 3,312
  • 1
  • 22
  • 32
  • if i do like this, its working for small size images, I am getting OutOfMemmoryError for big images, thanks – user1395885 May 17 '12 at 06:02
  • that is another question apart from this – dinesh sharma May 17 '12 at 06:06
  • thanks for ur answer, but its not another question, i forgot to add that comment to my question, can u please help me – user1395885 May 17 '12 at 06:27
  • OutOfMemory error depends on the device's memory size that you use. and to avoid this we can do 1 thing that before performing this task check out the available memory in your device and if you find less then 16 mb give the warning message of memory low – dinesh sharma May 17 '12 at 06:34
  • to avoid that OME only Am doing like this. is any chance for this – user1395885 May 17 '12 at 06:56
0

This is a problem of memory leaks.

You'll find a solution here:

Strange out of memory issue while loading an image to a Bitmap object

Community
  • 1
  • 1
gutiory
  • 1,135
  • 5
  • 13
  • 33
0

To get image from the Gallery Please try the below code

public class SelectPhotoFromGallery extends Activity 
{

private static final int SELECT_PICTURE = 1;
private String selectedImagePath="";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  intent.addCategory(Intent.CATEGORY_OPENABLE);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  startActivityForResult(intent, SELECT_PICTURE); 
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE)
        {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);

        File imgFile = new  File(selectedImagePath);
 if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);

 }}}}}
Ponmalar
  • 6,871
  • 10
  • 50
  • 80
  • this is also working same as my code, i dint find any change on it, but u have don a valuable help. can you please tell me, how can i reduce img size – user1395885 May 17 '12 at 07:56
  • check this http://android-coding.blogspot.in/2011/06/reduce-bitmap-size-using.html – Ponmalar May 17 '12 at 08:19
0

Have you tried with decodeByteArray instead?

user1302884
  • 783
  • 1
  • 8
  • 16