0

I'm using the afilechooser module to select a file from the user's device. The device has both internal storage and external storage. when I select a file from internal storage the path is returned correctly the uri looks like this:

Uri =content://com.android.externalstorage.documents/document/primary%3Amyfile.txt

However, the afilechooser fails when I select a file from the external sd card and the URI looks like this:

Uri = content://com.android.externalstorage.documents/document/3935-6562%3Amyfile.txt

So I was looking at the code in the afilechooser getPath method and it has this condition:

 if ("primary".equalsIgnoreCase(type)) {

                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

Now I can see that it's not getting the path because instead of containing primary it contains "3935-6562" Now I can just write another condition to test for this but I am wondering. On this device it's "3935-6562" But on a different device would it still be the same numbers? If they are different depending on device how can I make it so it will work? Also If the "3935-6562" is something that is the same across all devices how would I get the path to it since Environment.getExternalStorageDirectory() seems to be returning the internal sd path, not the external

Thanks in advance. I'm still learning this stuff so please be as simple as possible.

graffixnyc
  • 1
  • 1
  • 8

1 Answers1

0

I found this as a solution, not sure if it's the best approach though.. Any input would be appreciated

if ("primary".equalsIgnoreCase(type)) {
     Log.i ("PRIMARY", Environment.getExternalStorageDirectory() + "/" + split[1]);
     return Environment.getExternalStorageDirectory() + "/" + split[1];
}
else {
     String sdpath=null;

     if(new File("/storage/extSdCard/").exists()){
          sdpath="/storage/extSdCard/";
          Log.i("Sd Cardext Path",sdpath);
     }
     if(new File("/storage/sdcard1/").exists()){
          sdpath="/storage/sdcard1/";
          Log.i("Sd Card1 Path",sdpath);
     }

     Log.i ("EXT", sdpath + split[1]);
     return sdpath + split[1];
}
graffixnyc
  • 1
  • 1
  • 8