简体   繁体   中英

open a file only from the application that own created

Is there a solution to create an android application where there is a download system and the file can only be accessed in that application, for example an audio and video file. Just like Joox or Spotify, when users download music, only in each application can access the music.

It's possible, when you store files in your App's private directory. Example in documentation: https://developer.android.com/training/data-storage/files#WriteInternalStorage

val sourceFile = File("Absolute/Path/To/Source/File")
val privateFileName = "privatefile"
context.openFileOutput(privateFileName, Context.MODE_PRIVATE).use {
    it.write(sourceFile.readBytes())
}
val newFile = File(context.filesDir, privateFileName)
if (newFile.exists() && newFile.length > 0) {
    //You succeeded.
}

This will create a file in private folder (Code is in Kotlin). Only your app can access it. Inside stream you can write to it any data you want.

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