Open android image in intent causes StaleDataException

132 views Asked by At

I tried using:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + file_to_open), "image/*");
context.startActivity(intent);

or

Intent intent = new Intent();                   intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(file_to_open)),"image/jpeg");
context.startActivity(intent);

and it works, but when I try leaving the image viewing intent, I get a RuntimeException, StaleDataException Attempted to access a cursor after it has been closed

when I try launching a different intent, it works, so it's not related to pausing or resuming my activity

please someone help

turns out that it's other intents too, like email intent, when canceled, makes that error

1

There are 1 answers

0
Amir.F On

found the answer. it had nothing to do with the intent itself, but with the fact I was using the following method:

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String path = cursor.getString(column_index);
    cursor.close();
    cursor = null;
    return path;
}

notice that the managedQuery has been deprecated, and even if you do decide to use it, you should never close the cursor or set it to null as seen in the following answer:

Unable to resume Activity error