OpenInputStream返回文件未找到异常,但文件存在
问题描述:
我想从SD卡扩展位图并将其写入手机内存。然后在稍后解码它将其添加到HashMap
。OpenInputStream返回文件未找到异常,但文件存在
的问题是,我收到文件未发现异常尽管路径是正确的,缩放后的图像存在(我检查)
这里是节约部分
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream, null, options);
File imageRootPath = getFilesDir();
File imageRoot = new File(imageRootPath, imagUri.getLastPathSegment()+".png");
FileOutputStream out = new FileOutputStream(imageRoot);
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 90, out);
这里是部分时,我读文件
try {
Uri mainImgeUri = Uri.parse(imageRoot.toString());
File imageFile = new File(mainImgeUri.toString());
if(imageFile.exists()){
System.out.println("it does");
}
InputStream imageStream = ListPropertiesBaseActivity.this.getContentResolver().openInputStream(mainImgeUri); // I am getting file not found error
Bitmap yourSelectedImage =BitmapFactory.decodeStream(imageStream);
hmBitmap.put(ID, yourSelectedImage);
imageStream.close();
} catch (Exception e) {
e.printStackTrace();
}
难道是OpenInputStream
不能从手机内部存储器中读出?或者可能是储蓄导致的形象不好?
虽然我能够通过手动浏览到该文件,并打开来查看它
请注意的System.out.println执行所以这意味着该文件存在
答
你试过记录imageStream来查看uri实际是什么吗?
答
使用创建File
对象你mainImgeUri
uri和检查文件是否存在,将该文件传递到openInputStream()
方法
+0
我把代码来检查文件是否存在。它确实如此。 OpenInputStream只接受URI,所以我不能传递文件,但文件存在 – Snake 2013-02-27 17:04:22
答
我找到了。那么我找到了一个解决方法,但我从来没有能够解释以前的behviour和为什么它不会工作。我换成这个getContentResolver与
FileInputStream fis = new FileInputStream(imageFile);
Bitmap yourSelectedImage =BitmapFactory.decodeStream(fis);
这个工作。任何想法为什么!虽然
那么,因为我在填充imageStream的行处发生异常,所以它甚至没有填充 – Snake 2013-02-27 17:32:26
我担心解析从imageRoot.toString()不会给你一个真正的URI到文件的位置。 您可以尝试Uri mainImgeUri = Uri.fromFile(imageRoot)以获取实际的文件:// uri与openInputStream一起使用。 – speedynomads 2013-02-27 17:41:59
我记录了URI并且它有正确的路径。我会用其他东西更新我的代码 – Snake 2013-02-27 17:47:09