Android Open External Storage Directory(Sdcard) for Storing File

How can I get the external SD card path for Android 4.0+?

I have a variation on a solution I found here

public static HashSet<String> getExternalMounts() {
final HashSet<String> out = new HashSet<String>();
String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
String s = "";
try {
final Process process = new ProcessBuilder().command("mount")
.redirectErrorStream(true).start();
process.waitFor();
final InputStream is = process.getInputStream();
final byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
s = s + new String(buffer);
}
is.close();
} catch (final Exception e) {
e.printStackTrace();
}

// parse output
final String[] lines = s.split("\n");
for (String line : lines) {
if (!line.toLowerCase(Locale.US).contains("asec")) {
if (line.matches(reg)) {
String[] parts = line.split(" ");
for (String part : parts) {
if (part.startsWith("/"))
if (!part.toLowerCase(Locale.US).contains("vold"))
out.add(part);
}
}
}
}
return out;
}

The original method was tested and worked with

  • Huawei X3 (stock)
  • Galaxy S2 (stock)
  • Galaxy S3 (stock)

I'm not certain which android version these were on when they were tested.

I've tested my modified version with

  • Moto Xoom 4.1.2 (stock)
  • Galaxy Nexus (cyanogenmod 10) using an otg cable
  • HTC Incredible (cyanogenmod 7.2) this returned both the internal and external. This device is kinda an oddball in that its internal largely goes unused as getExternalStorage() returns a path to the sdcard instead.

and some single storage devices that use an sdcard as their main storage

  • HTC G1 (cyanogenmod 6.1)
  • HTC G1 (stock)
  • HTC Vision/G2 (stock)

Excepting the Incredible all these devices only returned their removable storage. There are probably some extra checks I should be doing, but this is at least a bit better than any solution I've found thus far.

Storing a file on Internal And External SdCard

Finally found a working solution , but its not recomended, to get to external sd card i used hard coded path , like /storage/extSdCard/StorageTest/input but this path depends upon device , the above path works in Samsung Galaxy note series but for xperia Z its /storage/removable/sdcard1. This solution worked for me because my client use a specific device.But like this you cant create a global method which works on every device, so here is the code which worked for me

String galaxy_note = "/storage/extSdCard";
File file = new File(galaxy_note
+"/StorageTest/input");

you can also check if there is a removable sd card installed in device or no by using

Environment.isExternalStorageRemovable();

Android Accessing get external directory

problem solved.
The problem is related with new android SDK. We need to add additional permission.

Thanks everyone.

Getting the path to SD card

The only way I found is to semi-hardcode it:

File[] folders = myappcontext.getExternalCacheDirs();

gives you the path to the "cache" folders your app has access to (but that are deleted when you uninstall your app).

If the phone uses a removable SD card (that is currently mounted), the length of the array should be "2":

  1. The path to the "cache" folder in the external (not removable) storage
  2. The path to the "cache" folder on your SD card

They look something like this:

/storage/emulated/0/Android/data/com.mycompany.myapp/cache
/storage/xxxx-xxxx/Android/data/com.mycompany.myapp/cache

... where "x" is the number (id?) of your sd card. I've only been able to test it with 2 different SD cards and both had their own number.

Environment.getExternalStorageDirectory();

should also give you

/storage/emulated/0/

which is the non-hardcoding way of getting access to the external storage. ;)

If you create a new folder on the very first level of your SD card on your PC, its path will be:

/storage/xxxx-xxxx/myfolder

I also have to warn you: While you can read the "myfolder" folder, you can't write in it (will just throw an "Access Denied" exception with Android 7) because of the changes to the whole system that came with Kitkat. But that's a different problem I'm going to address in a new question.



Related Topics



Leave a reply



Submit