错误:
致命例外:主要 java.lang.NullPointerException at java.io.File.fixSlashes(File.java:185)
在java.io.File。(File.java:134) at ua.romanpotapskiy.antihawk.prokaton.DeliverActivity.galleryAddPic(DeliverActivity.java:308) 在 ua.romanpotapskiy.antihawk.prokaton.DeliverActivity.onClick(DeliverActivity.java:246) 在android.view.View.performClick(View.java:3511)
在android.view.View $ PerformClick.run(View.java:14105)
在android.os.Handler.handleCallback(Handler.java:605)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
在java.lang.reflect.Method.invoke(Method.java:511)
在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
在dalvik.system.NativeStart.main(本地方法)
代码:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent("android.media.action.IMAGE_CAPTURE");
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Toast.makeText(this, "Error!", Toast.LENGTH_SHORT).show();
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, 0);
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/Prokaton/Big");
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
mCurrentPhotoPath = image.getAbsolutePath();
imageName = image.getName();
...
return image;
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
此行错误: 文件f =新文件(mCurrentPhotoPath);
Android 4.0.3上的错误。在另一个版本工作正常。
答案 0 :(得分:1)
根据您发布的代码,我猜您在dispatchTakePictureIntent()
被调用之前遇到了错误,因为调用createImageFile()
方法设置mCurrentPhotoPath
变量的方法就是这个方法具有非空值。
一个简单的解决方法是使用非空检查更新galleryAddPic()
方法,如下所示:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
if (mCurrentPhotoPath == null) {
return;
}
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
答案 1 :(得分:0)