我使用以下方法来防止内存不足异常,但是Bitmap ist始终为null。有人知道吗?
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;
}
最佳答案
是。这是Google的另一个错误。解决方案是这样做:
bm = BitmapFactory.decodeStream(new FileInputStream(fileDescriptor));
代替
bm = BitmapFactory.DecodeFileDescriptor(fileDescriptor.FileDescriptor, null, options);