我正在开发一个允许拍照并将它们保存到 SD 卡上的目录中的应用程序。它在大多数设备上都可以正常工作,但在极少数设备上出现此错误:
08-26 15:29:11.712 11925 11925 E AndroidRuntime: FATAL EXCEPTION: main
08-26 15:29:11.712 11925 11925 E AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data dat=file:///storage/emulated/0/.MyImgs/IMG_20132926032905.jpg typ=image/jpeg (has extras) }} to activity {com.example.myApp/com.example.myApp.Com}: java.lang.NullPointerException
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3403)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3446)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread.access$1100(ActivityThread.java:150)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.os.Looper.loop(Looper.java:213)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5153)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: Caused by: java.lang.NullPointerException
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1445)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1389)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:472)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.content.ContentResolver.notifyChange(ContentResolver.java:1297)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.content.ContentResolver.notifyChange(ContentResolver.java:1286)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.content.ContentResolver.notifyChange(ContentResolver.java:1266)
-->08-26 15:29:11.712 11925 11925 E AndroidRuntime: at com.example.myApp.Com.onActivityResult(Com.java:265)<--
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.Activity.dispatchActivityResult(Activity.java:5293)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3399)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: ... 11 more
这是我创建文件夹、保存图像和启动相机的方法
String root = Environment.getExternalStorageDirectory()+"/.MyImgs/";
File newDirectory = new File(root);
//create directory if it doesn't exist
if(!newDirectory.exists())
newDirectory.mkdirs();
//create and name the image
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
File f = new File(newDirectory,"IMG_"+date+".jpg");
//save image directory
mUri = Uri.fromFile(f);
// Result of activity: TAKE_PICTURE
startActivityForResult(i, TAKE_PICTURE);
这是发生错误的部分:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
getContentResolver().notifyChange(mUri, null);//line 265
}
}
谁能帮我找出问题所在?为什么它在某些设备上有效,而在其他设备上无效?谢谢。