Android - 无法访问下载的文件

时间:2015-11-22 11:08:05

标签: java android

我试图编写一个使用Android DownloadManager下载文件的应用程序,完成下载后,我想编辑该文件。尝试使用以下代码访问文件时,我遇到了问题:

File file = new File(context.getFilesDir(), filename);
file.exists() - returns false even though I can view the file with the File Manager app

注意:以上行是使用OnRecieve()方法在BroadcastReciever中写入的,而我使用的设备没有外部存储空间。

我尝试使用createNewFile()方法检查代码查找文件的位置,这样exists()方法返回true我无法生活在文件管理器应用程序中找到创建的文件。

我还尝试了以下URI:

/storage/emulated/0/Android/data/com.example.max.lucas/files/filename
/emulated/0/Android/data/com.example.max.lucas/files/filename
/0/Android/data/com.example.max.lucas/files/filename
/Android/data/com.example.max.lucas/files/filename
/data/com.example.max.lucas/files/filename
/com.example.max.lucas/files/filename
/files/filename
/filename

但似乎都没有效果。

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.max.lucas" >

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >

    <activities>.....</activities>

    <receiver                 <- Where I'm trying to find and edit the file
        android:name=".rcv_DownloadedFilesHandler"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
            <action android:name="com.example.max.lucas"/>
        </intent-filter>
    </receiver>
</application>

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

  

一切都在内部存储上

不是从Android SDK的角度来看。就Android SDK而言,Internal storage不是external storage,也不是removable storage

  

将文件下载到/storage/emulated/0/Android/data/com.example.max.lucas/files/filename

相当于new File(getExternalFilesDir(null), "filename"),假设您的应用在设备的主要用户帐户中运行(通常就是这种情况)。

getFilesDir()指向您可以在应用内部存储空间存储任意文件的位置。您的文件位于应用程序特定位置的外部存储上。