简体   繁体   中英

Android - can't access a downloaded file

I'm trying to write an app that downloads a file with the Android DownloadManager and when done with the download, I want to edit the file. I'm facing a problem when trying to access the file with the following code:

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

Note: The above lines are written in a BroadcastReciever in the OnRecieve() method and the device I'm using does not have external storage.

I tried checking where the code looks for the file by using the createNewFile() method yet while that makes the exists() method return true I can't for the life of me find the created file in the File Manager app.

I have also tried the following URIs:

/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

yet none seemed to work.

My manifest:

<?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>

Am I missing something?

Everything is on the internal storage

Not from the standpoint of the Android SDK. Internal storage is not external storage , and neither are removable storage , in terms of the Android SDK.

the file is downloaded to /storage/emulated/0/Android/data/com.example.max.lucas/files/filename

That equates to new File(getExternalFilesDir(null), "filename") , assuming that your app is running in the device's primary user account (which is typically the case).

getFilesDir() points to a location where you can store arbitrary files in your app's corner of internal storage. Your file is on external storage, in an app-specific location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM