简体   繁体   中英

Android restricting downloaded files to app

The app I'm building is for playing videos, but it needs the ability to download the videos so they can be watched when the user is not connected to the internet.

Whenever my phone downloads a file it goes to the phone's downloads folder, but I want the downloaded file to only be able to be accessed from the app.

Can someone point me in the right direction? All my searches lead to apps for downloading videos.

If you are handling files that are not intended for other apps to use (such as graphic textures or sound effects used by only your app), you should use a private storage directory on the external storage by calling getExternalFilesDir().

This method also takes a type argument to specify the type of subdirectory (such as DIRECTORY_MOVIES). If you don't need a specific media directory, pass null to receive the root directory of your app's private directory.

Beginning with Android 4.4, reading or writing files in your app's private directories does not require the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions. So you can declare the permission should be requested only on the lower versions of Android by adding the maxSdkVersion attribute:

<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                 android:maxSdkVersion="18" />
...
</manifest>

When the user uninstalls your application, this directory and all its contents are deleted.当用户卸载您的应用程序时,该目录及其所有内容都将被删除。 Also, the system media scanner does not read files in these directories, so they are not accessible from the MediaStore content provider. As such, you should not use these directories for media that ultimately belongs to the user, such as photos captured or edited with your app, or music the user has purchased with your app—those files should be saved in the public directories.

Refer: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

using getFilesDir() would work. Check this image from official android docs

but using getExternalFilesDir files were still accessible using file manager in android -> package_name_folder, by using getFilesDir we cannot access those from outside, file manager included.

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