0

I use the following method to prevend a outofmemory exception, but the Bitmap ist always null. Has anybody an idea?

public Bitmap readBitmap(Android.Net.Uri selectedImage) {

        Bitmap bm = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.InSampleSize = 9;
        AssetFileDescriptor fileDescriptor = null;
        try {
            fileDescriptor =  this.ContentResolver.OpenAssetFileDescriptor(selectedImage,"r");
        } catch (FileNotFoundException e) {
            Toast.MakeText(this, e.Message, ToastLength.Long);
        }
        finally{
            try {
                bm =  BitmapFactory.DecodeFileDescriptor(fileDescriptor.FileDescriptor, null, options);
                fileDescriptor.Close();
            } catch (IOException) {
            }
        }
        return bm;
}
anguish
  • 458
  • 1
  • 7
  • 27

4 Answers4

1

Yes. This is another bug of Google. Solution is to do this:

bm =  BitmapFactory.decodeStream(new FileInputStream(fileDescriptor));

Instead of

bm =  BitmapFactory.DecodeFileDescriptor(fileDescriptor.FileDescriptor, null, options);
Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69
0

BitmapFactory.DecodeFileDescriptor must be throwing an exception if bm is null.

Ameen
  • 2,576
  • 1
  • 14
  • 17
0

On your catch, try modifying like this

} catch (IOException e) {
   Log.v ("Message", ""+e.message);
        }

So you could see whats the error that it returns to null

Androyds
  • 391
  • 1
  • 3
  • 20
  • android.resource://My.Paketname/drawable/tabbar is my uri My.Paketname is of course the correct name – anguish Jan 07 '13 at 10:20
  • then refer on my code here http://stackoverflow.com/questions/12559481/downloaded-image-can-not-be-displayed/12559586#12559586 – Androyds Jan 08 '13 at 03:31
0

Perhaps the Uri is incorect and FileNotFoundException is thrown. But you can't see this because you are missing the show() method of the Toast in the catch clause.

Should be like this:

 Toast.MakeText(this, e.Message, ToastLength.Long).show();
Andy Res
  • 15,963
  • 5
  • 60
  • 96