views:

388

answers:

2

In reference to question

http://stackoverflow.com/questions/2147704/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?

A: 

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.

mbaird
thanks for the quick answer, can you quote soem example of saving the file to the SD Card
rob
I don't understand your last comment. Now you want an example that DOES save to the SD card?
mbaird
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
Just do the same thing I mentioned above, but give the path to the SD card.
mbaird
+1  A: 

Hi Rob.. 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
Christopher, you are a star, Thanks alot! i gonna trycheers
rob
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.icsAny suggestions?
rob
WARN/NotificationService(53): java.io.IOException: setDataSource failed.: status=0x80000000
rob
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
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
I have added android.Manifest.permission to my application but its still giving the same exception
rob