1

When creating an Android application, I have some files that needs to be stored on the android itself.

How do I do this?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Mahi Mali
  • 111
  • 3
  • 15
  • you can't access your phones internal memory thru DDMS. use assets folder in your app and copy to the internal memory by progrmatically. – Padma Kumar Jul 04 '12 at 08:39
  • @MahiMali : See this documentation about Data Storage...http://developer.android.com/guide/topics/data/data-storage.html – Squonk Jul 04 '12 at 08:41
  • Please, can you show me how to copy to the internal memory by progrmatically. – Mahi Mali Jul 04 '12 at 08:43
  • Actually i wanted to just create files first.After deployment in mobile that files should be on that mobiles system and can be read or write data from that created files.so can somebody help me. – Mahi Mali Jul 04 '12 at 08:56
  • @MahiMali look at this do it in your first boot screen: http://stackoverflow.com/a/4530294/1012284 – Padma Kumar Jul 04 '12 at 08:56
  • @PadmaKumar: No i don't want to copy my files into sdcard.My question is, `if i create files in assets folder then can they are also be present in my android phone's internal memory after deploy?` – Mahi Mali Jul 04 '12 at 09:02
  • @MahiMali you need to copy instead of SDcard put location as data/data/your.package/files/ – Padma Kumar Jul 04 '12 at 09:18
  • 1
    @PadmaKumar : Please don't advise people to hard-code a path such as `data/data/your.package/files/` - it is not guaranteed to work. There are helper methods to find internal and external directories which are explained in the link I posted in my comment above. – Squonk Jul 04 '12 at 10:03

3 Answers3

3

If you have local files, like some error tones, some openning video.. then place it in you assets folder of your project.

If you have dynamic data need to download at run time then use this guide.

Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
1

Best place for generic files would be the assets folder.

You can access files through the AssetManager, which you can get with Activity.getAssets() for example.

Here is an example how you could access a text file:

try {
    BufferedReader br = new BufferedReader(
        new InputStreamReader(getAssets().open("sometextfile.txt")));

        // do stuff with br
} catch (IOException e) {
        e.printStackTrace();
}

For more information on AssetManager read the Java Doc. Oh and yes, you can create folders in assets.

Don Ho
  • 1,289
  • 10
  • 18
  • Please,can you tell me about writing and reading files from assets folder.Can we create Folders in assets? – Mahi Mali Jul 04 '12 at 08:45
  • Please tell me , can we read and write both from assets folder? – Mahi Mali Jul 04 '12 at 09:12
  • No, you can not write to it. As Squonk pointed out above, you can read http://developer.android.com/guide/topics/data/data-storage.html to find out more about options to store files. – Don Ho Jul 04 '12 at 09:20
1

If you want to keep some files like readme.txt or even music files, you can use the raw folder inside of res folder. So create a folder named raw inside of res folder.

Inside of raw folder, let us assume that there is a file named readme.txt, assuming that the Activity class is called MyActivity.

Now, you can read the contents of a file into a String as shown below:

StringBuilder strContents = new StringBuilder();
String thisLine;
InputStream is = MyActivity.this.getResources().openRawResource(R.raw.readme);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((thisLine = br.readLine()) != null) { // while loop begins here
    strContents.append(thisLine);
}
br.close();
//Now you have the data in strContents

Alternately, assets is also one such folder that you can use since the raw folder contains the file as is without any optimization, zipping done by Android.

So create an assets folder in your Project root folder and place your files there e.g. myfile.

Now, you can get an instance of the file InputStream as given below:

InputStream is = getBaseContext().getAssets().open("mydb");

Romin
  • 8,708
  • 2
  • 24
  • 28
  • Hey please answer me `can we read and write both from raw folder?` – Mahi Mali Jul 04 '12 at 09:31
  • 1
    As far as I know, you cannot write to a `raw` folder. You should use from one of the persistence options in Android : Files / Shared Preferences / Databases. This will create files in the sandboxed area for your application. – Romin Jul 04 '12 at 09:59