Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCarla Brooks
803 PointsSaving on External Storage not working
Hi there... I have a question I used this exact code by the app is saving the files on the Internal Storage, instead of the external (using an LG Spirit), I double check by clicking the properties of the image and I get this path: " / storage/emulated/0/Pictures/App Name", here's my code:
private Uri getOutputMediaFileUri(int mediaType) {
if (isExternalStorageAvailable()) {
//get Uri and return it
//1.Get external storage directory
File mediaStorageDir = new File(Environment.
getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "App Name");
//2.Create a subdirectory
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.e(TAG, "Failed to create directory");
return null;
}
}
File mediaFile;
Date now = new Date(); //this line and the following creates a date and a string of the specific time and place where taken the photos
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(now);
String path = mediaStorageDir.getPath() + File.separator;
switch (mediaType) {
case DeviceConstants.MEDIA_TYPE_PHOTO:
mediaFile = new File(path + "IMG_" + timeStamp + ".jpg");
break;
default:
return null;
}
Log.d(TAG, "File: " + Uri.fromFile(mediaFile));
//5.Return the file URI
return Uri.fromFile(mediaFile);
} else {
return null;
}
}
private boolean isExternalStorageAvailable() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
1 Answer
Floris De Feyter
17,174 PointsI think your idea about "external storage" is not entirely right. "External" doesn't just mean "SD Card", from the android developers site: "Every Android-compatible device supports a shared "external storage" that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage."
The static variable Environment.DIRECTORY_PICTURES will return a directory to your "internal" external memory (more specifically, where all your pictures are stored). So if you want to save the file to an SD Card, you need to give the file path to your SD Card. This can be something like: "/mnt/external_sd/" or "/mnt/extSdCard/". Normally
Environment.getExternalStorageDirectory();
returns the correct path to the SD Card.
So then try (line 5):
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "App Name");
Carla Brooks
803 PointsCarla Brooks
803 PointsHi there many thanks for your response but data is still saving in the internal, ive check it and I dont know if there is an specific folder only in the sd card?