iPaulPro / aFileChooser

[DEPRECATED] Android library that provides a file explorer to let users select files on external storage.
Apache License 2.0
1.79k stars 850 forks source link

Get file path from external storage (SD card) #58

Open VaishaliBhujbal opened 9 years ago

VaishaliBhujbal commented 9 years ago

How can i get file path from sd card. It is not possible in the code which is given

xandebianchi commented 8 years ago

I did a modification in function getPath, FileUtils.java. It could solve the problem. I propose to solution and up it.

... if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } else { String strPath; if (Environment.isExternalStorageRemovable()){ strPath = System.getenv("EXTERNAL_STORAGE"); } else{ strPath = System.getenv("SECONDARY_STORAGE"); if (strPath == null || strPath.length() == 0) { strPath = System.getenv("EXTERNAL_SDCARD_STORAGE"); } } ...

joshiparthin commented 8 years ago

The code which you have written is simply giving mounting point of SD card. It is not path of file selected from chooser.

riten7 commented 7 years ago

Ya I am also facing the same issue with this library...

svenoaks commented 7 years ago

Here's a way to get the file path for secondary storage volumes, it is relatively non-hacky and should work on every device. Modify FileUtils.getPath(). I have tested it on Samsung SIII mini with Android 4.4 and Moto X Pure and ZTE Axon 7 with Android 6.0 so far.

else if (isExternalStorageDocument(uri))
            {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type))
                {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
                else
                {
                    final int splitIndex = docId.indexOf(':', 1);
                    final String tag = docId.substring(0, splitIndex);
                    final String path = docId.substring(splitIndex + 1);

                    String nonPrimaryVolume = getPathToNonPrimaryVolume(context, tag);
                    if (nonPrimaryVolume != null)
                    {
                        String result = nonPrimaryVolume + "/" + path;
                        File file = new File(result);
                        if (file.exists() && file.canRead())
                        {
                            return result;
                        }
                    }
                }

            }
@RequiresApi(Build.VERSION_CODES.KITKAT)
    public static String getPathToNonPrimaryVolume(Context context, String tag)
    {
        File[] volumes = context.getExternalCacheDirs();
        if (volumes != null)
        {
            for (File volume : volumes)
            {
                if (volume != null)
                {
                    String path = volume.getAbsolutePath();
                    if (path != null)
                    {
                        int index = path.indexOf(tag);
                        if (index != -1)
                        {
                            return path.substring(0, index) + tag;
                        }
                    }

                }
            }
        }

        return null;
    }

I think you could refract the string splitting there.

williams98 commented 7 years ago

Its not working in lollipop version and below. Its works prefectly for android N how to fix it with lollipop version

svenoaks commented 7 years ago

@williams98 Can you a name a [cheap] device it does not work on, I would like to investigate it.

DimitriosBerezhnoi commented 7 years ago

@svenoaks I have problem too with getting path of file from MicroSD Can u help me?

Tichex03 commented 7 years ago

Great code svenoaks ! Really saved me.... Looking for this code from 10 days searching on every sites....

DimitriosBerezhnoi commented 7 years ago

@sachinsshah , but it's no working in 4.2.2 5.1 and so on