通往SD卡的路径 [英] Getting the path to SD card

查看:105
本文介绍了通往SD卡的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在拒绝投票和/或将其标记为重复项之前,请先阅读整篇文章!

Please read the whole post before down-voting and/or marking it as a duplicate!

我正在开发一个应用程序,该应用程序可以从SD卡(如果有的话)或内置存储中读取用户手机上特定文件夹中的文件.是的,清单中提到了"READ_EXTERNAL_STORAGE",而且我还在处理API> 23的权限弹出窗口.

I'm working on an app that reads files from within a specific folder on the user's phone - either from the SD card (if there's one) or from the built in storage. Yes, the "READ_EXTERNAL_STORAGE" is mentioned in the manifest and I'm also handling the permission popup for API>23.

我曾经简单地使用

File folder = new File(Environment.getExternalStorageDirectory(), "myfolder");

获取存储在内置存储器(S7为32gb)中的文件夹的路径,但是现在我想获取SD卡的路径.根据Google给我的几乎所有结果,"Environment.getExternalStorageDirectory()"应该可以为您提供SD卡的路径,但对我而言却没有(而且永远也没有).

to get the path of the folder that is stored in the built in storage (32gb for an S7) but now I want to get the path to the SD card. According to pretty much every result google gave me, "Environment.getExternalStorageDirectory()" is supposed to give you the path to the SD card but for me it doesn't (and never has).

我已经使用两种不同的Samsung Galaxy S7(分别使用Android 7.0,一种带有SD卡(+文件夹),另一种没有SD +(+文件夹))进行了以下测试:

I've tested the following with two different Samsung Galaxy S7s, both with Android 7.0, one with an SD card (+ the folder), the other without (+ the folder):

Log.d(tag, System.getenv("EXTERNAL_STORAGE"));
Log.d(tag, System.getenv("SECONDARY_STORAGE"));
Log.d(tag, ""+new File(System.getenv("EXTERNAL_STORAGE")+File.separator+"myfolder").isDirectory());
Log.e(tag, ""+new File(System.getenv("EXTERNAL_STORAGE")+File.separator+ordner).getAbsolutePath());
Log.d(tag, Environment.isExternalStorageRemovable());
Log.d(tag, Environment.getExternalStorageDirectory());
Log.d(tag, Environment.getExternalStorageDirectory().getAbsolutePath());

令我惊讶的是,这两款手机都输出了相同的信息:

To my surprise both phones output the same infos:

/sdcard
null
true
/sdcard/myfolder
false
/storage/emulated/0
/storage/emulated/0

根据文件管理器应用程序(我的文件"),内置存储称为内部存储",这没有什么意义(我知道Android中内部存储和外部存储之间的区别).

According to the file manager app ("My Files"), the built in storage is called "Internal Storage", which makes even less sense (I know the difference between Internal and External Storage in Android).

如何获得实际SD卡的路径(不对其进行硬编码)?

How do I get the path to the actual SD card (without hardcoding it)?

推荐答案

我发现的唯一方法是对它进行半硬编码:

The only way I found is to semi-hardcode it:

File[] folders = myappcontext.getExternalCacheDirs();

为您提供了应用程序有权访问的缓存"文件夹的路径(但在您卸载应用程序时将被删除).

gives you the path to the "cache" folders your app has access to (but that are deleted when you uninstall your app).

如果电话使用可移动SD卡(当前已安装),则阵列的长度应为"2":

If the phone uses a removable SD card (that is currently mounted), the length of the array should be "2":

  1. 外部(不可移动)存储中缓存"文件夹的路径
  2. SD卡上缓存"文件夹的路径

它们看起来像这样:

/storage/emulated/0/Android/data/com.mycompany.myapp/cache
/storage/xxxx-xxxx/Android/data/com.mycompany.myapp/cache

......,其中"x"是您的SD卡的编号(id?).我只能用2个不同的SD卡进行测试,并且两个都有自己的编号.

... where "x" is the number (id?) of your sd card. I've only been able to test it with 2 different SD cards and both had their own number.

Environment.getExternalStorageDirectory();

还应该给你

/storage/emulated/0/

这是访问外部存储的非硬编码方式. ;)

which is the non-hardcoding way of getting access to the external storage. ;)

如果您在PC上SD卡的第一级上创建一个新文件夹,则其路径将为:

If you create a new folder on the very first level of your SD card on your PC, its path will be:

/storage/xxxx-xxxx/myfolder

我还必须警告您:虽然您可以读取"myfolder"文件夹,但由于其中对整个系统所做的更改,因此无法在其中写入(Android 7只会抛出"Access Denied"异常)奇巧(Kitkat)来了.但这是另一个问题,我将在一个新问题中解决.

I also have to warn you: While you can read the "myfolder" folder, you can't write in it (will just throw an "Access Denied" exception with Android 7) because of the changes to the whole system that came with Kitkat. But that's a different problem I'm going to address in a new question.

这篇关于通往SD卡的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆