錯誤:的NullPointerException在附加圖片庫
FATAL EXCEPTION: main
java.lang.NullPointerException at java.io.File.fixSlashes(File.java:185)
at java.io.File.(File.java:134) at ua.romanpotapskiy.antihawk.prokaton.DeliverActivity.galleryAddPic(DeliverActivity.java:308) at ua.romanpotapskiy.antihawk.prokaton.DeliverActivity.onClick(DeliverActivity.java:246) at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
代碼:這條線
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上的錯誤。在其他版本工作正常。
有一個在你的文件路徑 –
斜線一些問題的錯誤清楚地表明,'mCurrentPhotoPath'是空,因此您需要在調用'galleryAddPic()'方法之前爲其指定一個非空值。 – Saheed