Read and write file

I downloaded jpg from the network, I would like to save it to disk, and then download it again. I am 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 all the time I get an error message in the second line when trying to create a new instance of File ():

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

I am using emulator 2.0 and have sdcard. What am I doing wrong?

thank

+3
source share
2 answers

I have a test application with an SD card, so I added a second line to it, and it worked fine:

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

Are you sure you created the card correctly, and if you use Eclipse, did you tell Eclipse about the card (-sdcard \ sdcard.iso)?

+1
source

, getContentResolver(). openOutputStream(). FileOutputStream. Uris.

+2

Source: https://habr.com/ru/post/1734599/


All Articles