Get file path from external storage (SD card)
VaishaliBhujbal opened this issue · comments
How can i get file path from sd card.
It is not possible in the code which is given
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");
}
}
...
The code which you have written is simply giving mounting point of SD card. It is not path of file selected from chooser.
Ya I am also facing the same issue with this library...
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.
Its not working in lollipop version and below. Its works prefectly for android N how to fix it with lollipop version
@williams98 Can you a name a [cheap] device it does not work on, I would like to investigate it.
@svenoaks I have problem too with getting path of file from MicroSD
Can u help me?
Great code svenoaks ! Really saved me.... Looking for this code from 10 days searching on every sites....
@sachinsshah , but it's no working in 4.2.2 5.1 and so on