Mohammad Reza Majid Pour Mohammad Reza Majid Pour - 14 days ago 9
Android Question

strange error in aide compiler open input stream needs to be declared

i am using Aide and its giving me this strange error on openInputStream:

The Exception 'java.io.FileNotFoundException' must be caught or declared in the throws clause


my code:

case R.id.album:
intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, R.id.album);
break;
case R.id.camera:
intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra("output", Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "camera.jpg")));
startActivityForResult(intent, R.id.camera);
break;





protected void onActivityResult(int request, int result, Intent data) {
switch (request) {
case R.id.album:
if (result == -1) {
this.paintView.setPicture(BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData())));

}
case R.id.camera:
if (result == -1) {
try {
File file = new File(Environment.getExternalStorageDirectory(), "camera.jpg");
this.paintView.setPicture(BitmapFactory.decodeFile(file.toString()));
file.delete();
} catch (Exception e) {
}
}


i didnt find anything wrong in my code maybe Aide needs more code or its not support this kind of code.
i tried to figure it out from other questions similiar to this but i found nothing.
is there any replace for the code?

Answer

The problem with your code is that you are calling a method that could throw a FileNotFoundException ... but you are not catching the exception in an exception handler that surrounds the call.

I expect it is here:

this.paintView.setPicture(BitmapFactory.decodeStream(
    getContentResolver().openInputStream(data.getData())));

I would also note that code like this:

try {
    ...
} catch (Exception e) {
    // do nothing!
}

is called "exception squashing". It is Bad Practice. This kind of thing hides errors. 1) It typically allows the "damage" caused by the exception to spread to other parts of the application. 2) It makes accurate diagnosis the real cause of errors much harder.

And you are squashing Exception, which makes things worse.

References:

In short, if you habitually squash java.lang.Exception:

  • your code is likely to be unreliable
  • you won't know why, and
  • you won't be able to fix it.