在把bitmap写入到sdcard 后,我想更新系统相册,代码:
public void run() {
File saveFile = null;
try {
saveFile = prepareFile(this.mSaveFileName);
}catch(Exception e) {
mCallback.onSave(MyOnSaveCallback.State.CREATE_FAILED, null);
}
if( saveFile != null ) {
boolean result = false;
try{
result = savePictureToSD(mBitmap, saveFile);
if( result ) {
notifyMediaStoreToRefresh(saveFile);
}
}catch (Exception e) {
e.printStackTrace();
//log exception
}
if( result ) {
mCallback.onSave(MyOnSaveCallback.State.SAVE_SUCCESSED, saveFile);
} else {
mCallback.onSave(MyOnSaveCallback.State.SAVE_FAILED, saveFile);
}
}
}
private void notifyMediaStoreToRefresh(File savedFile) {
try {
MediaStore.Images.Media.insertImage(getContentResolver(),
savedFile.getAbsolutePath(), savedFile.getName(), null);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + savedFile.getAbsolutePath())));
} catch...
}
也就是在notifyMediaStoreToRefresh()方法里,报了OutOfMemoryError.一开始以为是Bitmap过大的原因(1.46M),但是在测试过程中发现有些不足1M的bitmap 也会发生这个错误,检查代码后,作如下更改,错误消失。
result = savePictureToSD(mBitmap, saveFile);
//here
mBitmap.recycle();
mBitmap = null;
if( result ) {
notifyMediaStoreToRefresh(saveFile);
}
that’s it.