Reputation: 11780
Does anyone know a way around this problem? I am using a camera intent to take pictures. The following method generates the file where the picture would be stored. However, mkdirs()
is not able to create the directory on the device. What are my options in such a situation?
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
return new File(mediaStorageDir.getPath() + File.separator + "IMG_"
+ timeStamp + ".jpg");
}
error log:
04-20 09:56:07.370: D/MyCameraApp(809): failed to create directory
04-20 09:56:07.370: D/AndroidRuntime(809): Shutting down VM
04-20 09:56:07.370: W/dalvikvm(809): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-20 09:56:07.441: E/AndroidRuntime(809): FATAL EXCEPTION: main
04-20 09:56:07.441: E/AndroidRuntime(809): java.lang.IllegalStateException: Could not execute method of the activity
04-20 09:56:07.441: E/AndroidRuntime(809): at android.view.View$1.onClick(View.java:3591)
04-20 09:56:07.441: E/AndroidRuntime(809): at android.view.View.performClick(View.java:4084)
04-20 09:56:07.441: E/AndroidRuntime(809): at android.view.View$PerformClick.run(View.java:16966)
04-20 09:56:07.441: E/AndroidRuntime(809): at android.os.Handler.handleCallback(Handler.java:615)
04-20 09:56:07.441: E/AndroidRuntime(809): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 09:56:07.441: E/AndroidRuntime(809): at android.os.Looper.loop(Looper.java:137)
04-20 09:56:07.441: E/AndroidRuntime(809): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-20 09:56:07.441: E/AndroidRuntime(809): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 09:56:07.441: E/AndroidRuntime(809): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 09:56:07.441: E/AndroidRuntime(809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-20 09:56:07.441: E/AndroidRuntime(809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-20 09:56:07.441: E/AndroidRuntime(809): at dalvik.system.NativeStart.main(Native Method)
04-20 09:56:07.441: E/AndroidRuntime(809): Caused by: java.lang.reflect.InvocationTargetException
04-20 09:56:07.441: E/AndroidRuntime(809): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 09:56:07.441: E/AndroidRuntime(809): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 09:56:07.441: E/AndroidRuntime(809): at android.view.View$1.onClick(View.java:3586)
04-20 09:56:07.441: E/AndroidRuntime(809): ... 11 more
04-20 09:56:07.441: E/AndroidRuntime(809): Caused by: java.lang.NullPointerException: file
04-20 09:56:07.441: E/AndroidRuntime(809): at android.net.Uri.fromFile(Uri.java:441)
04-20 09:56:07.441: E/AndroidRuntime(809): at com.mycompany.myapp.utils.FileUtils.getOutputMediaFileUri(FileUtils.java:19)
04-20 09:56:07.441: E/AndroidRuntime(809): at com.mycompany.myapp.MyActivity.dispatchCameraIntent(MyActivity.java:56)
04-20 09:56:07.441: E/AndroidRuntime(809): ... 14 more
04-20 09:56:10.860: I/Process(809): Sending signal. PID: 809 SIG: 9
Upvotes: 3
Views: 2461
Reputation: 1193
Have you added http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE and http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE to your manifest?
Upvotes: 2