0

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?

2 Answers2

3

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.

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.
Graham
  • 7,431
  • 18
  • 59
  • 84
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • More specifically, [openInputStream](https://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.Uri)) will throw the reported exception. Since it isn't a runtime exception, you have to catch it. – Jon Nov 25 '16 at 23:11
  • where should i add ' try { ... } catch (Exception e) { // do nothing! }' – Mohammad Reza Majid Pour Nov 25 '16 at 23:16
  • You shouldn't add it anywhere. Instead, you should READ the links I gave you so that you UNDERSTAND why doing that is such BAD PRACTICE. Then you figure out what the right thing to do is – Stephen C Nov 25 '16 at 23:22
  • thanks a lot bro i didnt tried it yet because its 3am in here but i belive its will gonna help me. – Mohammad Reza Majid Pour Nov 25 '16 at 23:30
  • Well bro. You should go to bed. You cannot learn to program at 3am. Ergo, if you are doing your homework at 3am, you won't learn anything. And since the *whole point* of homework is to learn, you are wasting your time. – Stephen C Nov 25 '16 at 23:35
0

You are trying to access a file or open a file, there is every possibility that FileNotFoundException would occur.

so you need to catch that error first and then any other kind of exception later.

So your code is supposed to be like this:

` switch (request) {
    case R.id.album:
        try{
if (result == -1) {    
this.paintView.setPicture(BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData())));
      }
 }
catch (FileNotFoundException e) {}
catch (Exception e){}`

or else you can throw the same to other method which is trying to access it.

with throws clause:

protected void onActivityResult(int request, int result, Intent data) throws FileNotFoundException{ //your code }

If you are using this method then you must see that, this exception is caught in some other method, which you are using.