我想处理当imageView
尝试从用户先前删除的本地文件中打开图像并改为显示默认图像时的情况。为此,我尝试:
int flag = 0;
try{
img_car.setImageURI(Uri.parse(path_picture));
flag = 1;
}
catch(Exception e){ //if I put FileNotFoundException I get error: Unreachable catch block for FileNotFoundException. This exception is never thrown from the try
// statement body
Log.i("TAG EXCEPTION PIC", e.getMessage());
img_car.setImageResource(pictureId);
}
img_car.setImageResource(pictureId);
和 img_car.setImageURI(Uri.parse(path_picture));
(当图片实际存在时)都有效。
我的问题是抓不到异常(当图片不存在时),基本上这个子句不会抛出它。(我的LogCat
不显示任何信息日志消息)。即便如此,LogCat
显示java.io.FileNotFoundException
:
09-06 13:12:22.066: W/ImageView(27274): Unable to open content:
file:///storage/sdcard0/folder_name/my_image.PNG
09-06 13:12:22.066: W/ImageView(27274): java.io.FileNotFoundException:
/storage/sdcard0/folder_name/my_image.PNG: open
failed: ENOENT (No such file or directory) 09-06 13:12:22.066:
W/ImageView(27274): at libcore.io.IoBridge.open(IoBridge.java:416)
09-06 13:12:22.066: W/ImageView(27274): at
java.io.FileInputStream.<init>(FileInputStream.java:78) 09-06
13:12:22.066: W/ImageView(27274): at
java.io.FileInputStream.<init>(FileInputStream.java:105) 09-06
13:12:22.066: W/ImageView(27274): at
android.content.ContentResolver.openInputStream(ContentResolver.java:445)
09-06 13:12:22.066: W/ImageView(27274): at
android.widget.ImageView.resolveUri(ImageView.java:631) 09-06
13:12:22.066: W/ImageView(27274): at
android.widget.ImageView.setImageURI(ImageView.java:379) 09-06
13:12:22.066: W/ImageView(27274): at
com.myapp....
指向我的行:img_car.setImageURI
。另一个奇怪的事情是,即使在上面的行上发生异常,该标志在执行1
之后也有值try-catch
,因此理论上不应执行该行。(对吗?)。任何帮助将不胜感激。谢谢!