【发布时间】:2019-07-15 09:42:27
【问题描述】:
我在运行 Android Oreo 的外部 sd 卡上写入时遇到一个奇怪的问题:
首先是常用命令 getExternalFilesDir(null).getAbsolutePath() 要么 Environment.getExternalStorageDirectory().getAbsolutePath()
返回模拟存储的路径,而不是真正的 sd 卡,有时称为辅助外部存储。
但这不是问题,因为我设法使用我帖子末尾描述的方法 getExternalStoragePath() 获取到 SD 卡的路径。
现在奇怪的是我无法让我的应用程序(假设包是 com.example.myapp)创建目录
SDCARDPATH/Android/data/com.example.myapp/files
尽管已正确授予所有必要的授权(但 WRITE_EXTERNAL_STORAGE 和 READ_EXTERNAL_STORAGE 可能仅适用于模拟存储,我觉得问题就在那里!)这是我正在做的事情:
String SDCARDPATH=getExternalStoragePath(this, true);
File md = new File(new File(new File(new File(SDCARDPATH,"Android"),"data"),PACKAGE_NAME),"files");
if(!md.exists()) {
try {
md.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
}
奇怪的是,如果我通过手机文件资源管理器手动创建目录,则应用程序可以在该目录中写入和读取而不会遇到任何问题。
所以我的问题是如何在我的外部 sdcard 上正确创建该目录
附注我看到我手机上安装的一些应用程序已经设法在 sd 卡上创建了他们的应用程序目录。
最后这里是我使用的getExternalStoragePath方法,是在网上抓到的:
提前感谢大家的帮助,
李
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;
}
【问题讨论】:
-
我也遇到了同样的问题,并且使用 andorid Q 中的存储访问框架情况变得更糟
-
@Pemba Tamang 你解决问题了吗?
-
我正在为一个 arduino 项目的个人制作一个应用程序,我刚刚硬编码了路径。对不起队友
-
I have a strange problem您正在使用反射。遇到“奇怪的问题”不要感到惊讶
标签: android