0

In reference to question

Can anyone explain the File() parameters used to download file in android?

Can I do it without creating virtual SD Card. Is there any way to save the file in phone memory in internal memory? if it is possible without using the virtual SD Card, then how?

Community
  • 1
  • 1
rob
  • 5,771
  • 4
  • 23
  • 26
  • 1
    Is there a reason you don't want to create a virtual SD card on your emulator? It takes like 2 seconds. – Mark B Jan 27 '10 at 16:35
  • Just keep in mind that the internal memory of the phone could be quite small (varies between handsets). – Erich Douglass Jan 27 '10 at 16:45
  • Well my project specification is like that, i can change it if it doesnot allow to do so with the phone memory – rob Jan 27 '10 at 16:57

2 Answers2

1

your question is a bit unclear about which you want to use, so here's both.

Like mbaird said, you can easily save a file to the phone's internal storage using Context.openFileOutput(). For example:

// Create file on internal storage and open it for writing
FileOutputStream fileOut;
try {
    fileOut = openFileOutput(userId +".ics", Context.MODE_PRIVATE);
} catch (IOException e) {
    // Error handling
}

// Write to output stream as usual
// ...

This would create a new file on the phone's internal storage, at a path like this:
/data/data/com.example.yourpackagename/files/123456.ics.

Only your application can read this file; others will not be able to read this file, like they would if it was on the SD card.


If you want to save a file to the SD card, you need something like this:

if (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED) {
    // SD card is not available
    return;
}

File root = Environment.getExternalStorageDirectory() +"/myapp/";
File newFile = new File(root, userId +".ics");
FileOutputStream fileOut = new FileOutputStream(newFile);

// Write to output stream as usual
// ...

As you can see, you cannot rely on an SD card being present and available for writing to at any given point in time. This could be for several reasons:

  • The device/emulator has no SD card
  • The SD card is being shared with the PC
  • The SD card is read-only
  • The SD card has no file system
  • The SD card is corrupt
Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • Christopher, you are a star, Thanks alot! i gonna try cheers – rob Jan 28 '10 at 17:04
  • Hi Christopher, I have tried to make the file and read it but getting these warnings as i tracked in CatLog WARN/System.err(223): java.net.SocketException: Permission denied (maybe missing INTERNET permission) WARN/System.err(223): java.io.FileNotFoundException: /data/data/com.example.helloandroid/files/345hde.ics Any suggestions? – rob Jan 29 '10 at 13:24
  • WARN/NotificationService(53): java.io.IOException: setDataSource failed.: status=0x80000000 – rob Jan 29 '10 at 13:28
  • I tried to look for the file in file Explorer, if its being created. openFileInput(String name) to open the file and specified the filename in it. It does show a file creation at the same time i run the application but doesn't show the filename, so how can i be assure that file is created. – rob Jan 29 '10 at 13:52
  • The first warning is self-explanatory.. you're missing the INTERNET permission to access the network. Read up on Android permissions; you need to add that to your manifest. As for the other stuff, it's unclear. If you're having specific issues unrelated to the basics of this question, it's best to ask them as a separate question. – Christopher Orr Jan 29 '10 at 14:19
  • I have added android.Manifest.permission to my application but its still giving the same exception – rob Jan 29 '10 at 15:47
0

If you do Context.openFileOutput() and give it just a filename like "myfile.txt" without a path, it should create the file in your app's data directory, not the SD card.

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • thanks for the quick answer, can you quote soem example of saving the file to the SD Card – rob Jan 27 '10 at 16:57
  • I don't understand your last comment. Now you want an example that DOES save to the SD card? – Mark B Jan 27 '10 at 19:13
  • yes I need an example to download a file to memory, as i can't really save the file to internal memory, now i will save the file to SD card memory.I need example for that – rob Jan 27 '10 at 22:28
  • Just do the same thing I mentioned above, but give the path to the SD card. – Mark B Jan 28 '10 at 01:47