4

有谁知道解决这个问题的方法?我正在使用相机意图拍照。以下方法生成将存储图片的文件。但是,mkdirs()无法在设备上创建目录。在这种情况下我有什么选择?

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");
}

错误日志:

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
4