如何从内部存储读取(子文件夹下载)? [英] How to read from the internal storage (subfolder Download)?
问题描述
经过大量搜索后,我无法找到如何从内部存储器(子文件夹下载)中读取配置文件(xml)的信息.我尝试访问/mnt/sdcard/Download/config.xml
,但收到异常.
After intensive search I could not find out how to read a configuration file (xml) from the internal storage (sub-folder download). I tried to access /mnt/sdcard/Download/config.xml
but received an Exception.
当试图从两个可能的供应商ApplicationContext
和Environment
获取可用文件夹时,我得到了以下信息:
When trying to get the available folders from the two possible suppliers ApplicationContext
and Environment
, I get these informations:
context.getFilesDir().getAbsolutePath(); // /data/data/com.xxx.yyy/files
context.getDir("Download", 0).getAbsolutePath();
// /data/data/com.xxx.yyy/app_Download
context.getDir("Download", 1).getAbsolutePath();
// /data/data/com.xxx.yyy/app_Download
Environment.getExternalStorageDirectory().getAbsolutePath();
// /storage/emulated/0
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
// /storage/emulated/0/Android/data/com.xxx.yyy/files/Download
Environment.getDataDirectory().getPath();
// /data
当我尝试这些信息以访问下载文件夹中的 config.xml (我通过Windows资源管理器手动复制文件)时,出现错误,例如FileNotFoundException或IllegalArgumentException.
When I try these information to access the config.xml in the Download-folder (I manually copied the file over the Windows explorer) I receive errors, e.g. FileNotFoundException or IllegalArgumentException.
我的文件在哪里,如何正确访问它?
Where is my file, and how can I access it properly?
推荐答案
-
在AndroidManifest.xml中设置读取权限:
Set the Read-Permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
使用以下代码:
Use the following code:
String filename = Environment.getExternalStorageDirectory().getPath() + File.separator + "download" + File.separator + "config.xml";
filename
用于任何您想做的事情.
filename
for whatever you want to do. 这篇关于如何从内部存储读取(子文件夹下载)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!