2

I am having some trouble with my application.

I wanted to download the files and then save them into my apps drawable folder, but since that is not an option because app is read only, I want to save the downloaded file into internal storage so that only my app can access them.
If I cannot directly download the file to internal storage, can I read from downloads folder and then save it to internal storage?

Either way, I have the download method, I just need to know where to save the downloaded file and how to access it at runtime and persistently store it in internal storage.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Pavle37
  • 525
  • 7
  • 22

2 Answers2

4

Try having a look at

_context.getFilesDir();

and

_context.getExternalFilesDir();
Unknownweirdo
  • 475
  • 6
  • 16
-1

/** * Copies a private raw resource content to a publicly readable * file such that the latter can be shared with other applications. */

private void copyPrivateRawResourceToPubliclyAccessibleFile() {
    InputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
        inputStream = getResources().openRawResource(R.raw.robot);
        outputStream = openFileOutput(SHARED_FILE_NAME,
                Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
        byte[] buffer = new byte[1024];
        int length = 0;
        try {
            while ((length = inputStream.read(buffer)) > 0){
                outputStream.write(buffer, 0, length);
            }
        } catch (IOException ioe) {
            /* ignore */
        }
    } catch (FileNotFoundException fnfe) {
        /* ignore */
    } finally {
        try {
            inputStream.close();
        } catch (IOException ioe) {
           /* ignore */
        }
        try {
            outputStream.close();
        } catch (IOException ioe) {
           /* ignore */
        }
    }
}
Cool
  • 10,754
  • 14
  • 59
  • 81
  • 1
    You don't understand, i want only my app to have access to the files, not the user and not other apps. – Pavle37 Apr 16 '14 at 11:40
  • @Pavle37 can you check my answer ! – Cool Apr 16 '14 at 15:29
  • What is R.raw.robot? Is that a file you've created in the raw folder in which you store data? And my downloaded file is located in Environment.DIRECTORY_DOWNLOAD, i think the outputStream won't read it if i just type in the name... Also my files are images and sounds. – Pavle37 Apr 16 '14 at 17:48
  • This bears absolutely no relationship to the question asked. – Chris Stratton Jan 22 '18 at 07:37
  • @ChrisStratton flaged to delete – Cool Jan 22 '18 at 09:34