[英]Unable to access external sd card on Android
I have easily accessed Internal Storage Files using我很容易使用访问内部存储文件
File internalStorage = new File( Environment.getExternalStorageDirectory().getAbsolutePath() );
internalStorage.listFiles();
And i am getting all the files.我正在获取所有文件。 But i am unable to get any file from my mounted sd-card.
但我无法从我安装的 SD 卡中获取任何文件。 i have checked.
我检查过。 if SD-Card is mounted
如果安装了 SD 卡
boolean isSdCard = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
And i have also tried different solutions like.我也尝试过不同的解决方案,比如。
File sdCardFolder = new File(System.getenv("SECONDARY_STORAGE"));
//RESULT.
sdCardFolder.getName() //sdcard0.
sdCardFolder.listFile() //null.
Here it's giving the folder name but sending null for listFile.这里它给出了文件夹名称,但为 listFile 发送 null。
And also i have tried different other methods to listFiles from my SD Card but unable to find a working one.而且我还尝试了其他不同的方法来列出我的 SD 卡中的文件,但找不到有效的方法。
in build.gradle.在 build.gradle 中。
compileSdkVersion 31.
targetSdkVersion 31
Check and ask for permission to access external cards in your manifest or by code在清单中或通过代码检查并请求访问外部卡的权限
After a lot of research and time consumed.经过大量的研究和时间消耗。 I finally got a solution for my Question.
我终于为我的问题找到了解决方案。 There are plenty of questions asked for the same problem but not any relevant and working is availible, up-till my exposure.
对于同一个问题,有很多问题被问到,但没有任何相关的,并且工作是可用的,直到我曝光。
I am answering my own question to help others facing same problem.我正在回答我自己的问题,以帮助其他面临同样问题的人。 I have used the following code to get the path of SD-Card.
我使用以下代码获取 SD 卡的路径。 And then i have accessed it normally as i have accessed internel storage of Android Device.
然后我正常访问它,因为我访问了 Android 设备的内部存储。
//THE FOLLOWING FUNCTION WILL RETURN THE PATH OF SD-Card, AND NULL IF NO SD CARD MOUNTED
public static String getExternalStoragePath(Context mContext, boolean is_removable) {
StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
Class<?> storageVolumeClazz = null;
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getPath = storageVolumeClazz.getMethod("getPath");
Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String path = (String) getPath.invoke(storageVolumeElement);
boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
if (is_removable == removable) {
return path;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
To add on to @Saqib's answer, I've simplified the code by removing the need to use his helper/wrapper class.为了补充@Saqib 的答案,我通过消除使用他的帮助器/包装器 class 的需要来简化代码。
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
List<StorageVolume> storageArray = storageManager.getStorageVolumes();
String path = "";
for (StorageVolume item : storageArray)
if (item.isRemovable())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
path = item.getDirectory().getPath();
To handle null case, simply check if the path string is empty or null and handle that appropriately.要处理 null 情况,只需检查路径字符串是否为空或 null 并适当处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.