I've read several questions and the Android Guide on saving files but can't figure out where I'm going wrong.
I'm trying to save an image to a new folder in the pictures directory of the SD Card.
I have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in the manifest.
It is throwing an exception in this method
String storageStage = Environment.getExternalStorageState();
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory (
Environment.DIRECTORY_PICTURES),"/WordResolver");
mediaStorageDir.mkdirs();
if (! mediaStorageDir.exists()){
if(! mediaStorageDir.mkdirs()) {
Log.d("WordResolver", "Failed to create directory");
return null;
}
}
//Creating Media File Name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File (mediaStorageDir.getPath() + File.separator + "IMG_WR" +
timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
The error it is throwing is a NullPointerException. The the reason it is a strange problem is because the program is trying to save in the internal storage rather than in the SD card as you can see here:
Where as I was under the impression that Environment.getExternalStoragePublicDirectory
would save to the external storage?
Here's the stack trace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aaron.wordresolver/com.example.aaron.wordresolver.cameraShot}: java.lang.NullPointerException: file
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:452)
at com.example.aaron.wordresolver.cameraShot.getOutputMediaFileUri(cameraShot.java:55)
at com.example.aaron.wordresolver.cameraShot.onCreate(cameraShot.java:45)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I'm using an emulated device if that matters!
Cheers