user1071138 user1071138 - 4 months ago 21
Android Question

Android application how to store and get a file inside the application

I have an application that make me choose a file through a file explorer(the file is stored on the sd), and then reads it.

I want to modify it, so it has the file directly into the app and reads the file from "inside". Where I have to put the file into the project? How can I access it?

Answer

Yes, you can put your file into /assets folder, and retrieve as follows:

AssetManager assetManager = getAssets();
InputStream instream = assetManager.open("file.txt");

or res/raw folder:

InputStream raw = getResources().openRawResource(R.raw.file);

If you want to modify it, you'll be able only to write a file into External storage (e.g. sdcard), or into Internal storage (under your application folder data/data/package_name/). If you store your file into External storage it will persist until user manually or programmatically deletes the file. But if you store this file into Internal storage, it will be deleted if user deletes an app, or clear an application cache.