user1071138 user1071138 - 1 year ago 87
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 Source

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

AssetManager assetManager = getAssets();
InputStream instream ="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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download