Reputation: 458
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;
}
Upvotes: 0
Views: 756
Reputation: 14612
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);
Upvotes: 1
Reputation: 16043
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();
Upvotes: 0
Reputation: 491
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
Upvotes: 0
Reputation: 2586
BitmapFactory.DecodeFileDescriptor
must be throwing an exception if bm is null.
Upvotes: 0