【发布时间】:2014-04-10 11:53:14
【问题描述】:
我正在使用 getBitmap 方法来显示图像。当我使用它作为方法时,如果它返回位图显示图像但如果它返回 null,则捕获异常。但是如果输入的 url 也是错误的,它应该处理 FileNotFoundException。如何处理两个异常并在 UI 中显示?
public Bitmap getBitmap(final String src) {
try {
InputStream stream = null;
URL url = new URL(src);
java.net.URL url = new java.net.URL(src);
URLConnection connection = url.openConnection();
InputStream input = connection.getInputStream();
myBitmaps = BitmapFactory.decodeStream(input);
return myBitmaps;
} catch (IOException e) {
e.printStackTrace();
Log.e("IO","IO"+e);
return null;
}
catch(OutOfMemoryError e1) {
e1.printStackTrace();
Log.e("Memory exceptions","exceptions"+e1);
return null;
}
}
在Activity中,我给过这样的
Bitmap filename=service.getBitmap(url_box.getText().toString());
if(file_name!=null)
{
displaybitmap(file_name);
}
else
{ //Toast.makeText("Memory Error");
//my question is how to handle two exception in UI where memory error and also
// when entered url is wrong, file not found exceptions also to handle.
}
【问题讨论】:
-
没有办法处理 NullPointer 异常。您必须避免空实例。
标签: android exception-handling bitmap out-of-memory filenotfoundexception