I'm learning Android development so am relatively new to some of the technical Android concepts, but I do have a strong developer background.
I am writing an app that I want to be able to directly access files on the user's SD card. The test device is a Samsung Galaxy Tab 4.
Samsung appears to use a custom method of mounting the SD card, placing it at /storage/extSdCard
rather than the more traditional /sdcard
.
In my app, I used the method Environment.getExternalStoragePublicDirectory
method to request the path for storage, and was given a path which points to the internal storage rather than the SD card. (/storage/sdcard0/...
)
When I try to directly access the SD card storage (/storage/extSdCard/...
), I get the error open failed: EACCES (Permission denied)
.
This device - and potentially any user's device - has very limited storage available on the internal memory. (This device has only 8GB of storage internally, most of which is taken up by apps.) Additionally, I want a potential app user to be able to put files onto their MicroSD card directly and not have to use an Android file transfer tool to load content into the app...
Any way I can access the external SD card directly from an app running on a Samsung device?
Test code:
public void doTest()
{
FileWriter fw;
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "test.txt");
Log.i("file","Trying to write a file at: "+f.getPath());
try {
fw = new FileWriter(f);
fw.write("You got a file.");
fw.flush();
fw.close();
Log.i("file", "Successfully wrote the file.");
}
catch (IOException e)
{
Log.i("file_error", "Could not write the file.");
Log.i("file_error",e.getMessage());
}
f = new File("/storage/extSdCard/Documents", "test.txt");
Log.i("file","Trying to write a file at: "+f.getPath());
try {
fw = new FileWriter(f);
fw.write("You got a file.");
fw.flush();
fw.close();
Log.i("file", "Successfully wrote the file.");
}
catch (IOException e)
{
Log.i("file_error", "Could not write the file.");
Log.i("file_error",e.getMessage());
}
}