如何从内部存储读取(子文件夹下载)? [英] How to read from the internal storage (subfolder Download)?

查看:120
本文介绍了如何从内部存储读取(子文件夹下载)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量搜索后,我无法找到如何从内部存储器(子文件夹下载)中读取配置文件(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.

当试图从两个可能的供应商ApplicationContextEnvironment获取可用文件夹时,我得到了以下信息:

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?

推荐答案

  1. 在AndroidManifest.xml中设置读取权限:

  1. 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用于任何您想做的事情.
  • Use filename for whatever you want to do.
  • 这篇关于如何从内部存储读取(子文件夹下载)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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