user246114
user246114

Reputation: 51711

Reading and writing a file

I downloaded a jpg from the net, I'd like to save it to disk then load it again. I'm trying something like this:

Bitmap bmp = ...; // loaded from net.
File file = new File(Environment.getExternalStorageDirectory(), "tmp.jpg");
OutputStream out = getContentResolver().openOutputStream(Uri.parse(file.getAbsolutePath())); 
bmp.compress(Bitmap.CompressFormat.JPEG, 70, out); 
out.flush();
out.close();

...

File f2 = new File(Environment.getExternalStorageDirectory(), "tmp.jpg");
Uri uri = Uri.fromFile(f2);

but I keep getting an error on the second line when trying to create the new File() instance:

java.io.FileNotFoundException: No content provider: /sdcard/tmp.jpg

I'm using a 2.0 emulator and it has an sdcard. What am I doing wrong?

Thanks

Upvotes: 3

Views: 1103

Answers (2)

AndroidRef.com
AndroidRef.com

Reputation: 326

I have a test app with an SD card, so I added your second line to it and it ran fine:

File file = new File(Environment.getExternalStorageDirectory(), "tmp.jpg");

Are you sure you created the card correctly, and assuming you're using Eclipse, did you tell Eclipse about the card (-sdcard \sdcard.iso)?

Upvotes: 1

Romain Guy
Romain Guy

Reputation: 98521

It's because you are using getContentResolver().openOutputStream(). You should just create a FileOutputStream instead. And you don't need to use Uris.

Upvotes: 2

Related Questions