读写文件

3

我从网络上下载了一个jpg文件,我想将它保存到磁盘上,然后再加载它。我正在尝试类似于这样的操作:

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);

但是在尝试创建新的File()实例时,我在第二行不断收到错误:

java.io.FileNotFoundException:没有内容提供程序:/sdcard/tmp.jpg

我正在使用一个带有SD卡的2.0模拟器。我做错了什么?

谢谢

2个回答

2

这是因为您使用了getContentResolver().openOutputStream()。您应该只需创建一个FileOutputStream即可。而且您不需要使用Uris。


1

我有一个带 SD 卡的测试应用程序,所以我将你的第二行添加到其中,它运行得很好:

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

你确定你正确地创建了卡片,并且假设你正在使用Eclipse,你是否告诉Eclipse关于这张卡片(-sdcard \sdcard.iso)?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接