0

I have following method to pass selected image path to another activity:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            Log.d("fsd", "onActivityResult: ");

            Intent intent = new Intent(getApplicationContext(), AddContentImage.class);
            Bundle b = new Bundle();
            b.putString("path", selectedImagePath);
            intent.putExtras(b);
            startActivity(intent);
        }
    }
}

Which is actually working fine, but I gets following error, when back button is pressed.

java.lang.RuntimeException: Unable to resume activity : android.database.StaleDataException: Attempted to access a cursor after it has been closed.
      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3169)
      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3247)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5582)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
      at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:63)
      at android.database.BulkCursorToCursorAdaptor.requery(BulkCursorToCursorAdaptor.java:132)
      at android.database.CursorWrapper.requery(CursorWrapper.java:228)
      at android.app.Activity.performRestart(Activity.java:6363)
      at android.app.Activity.performResume(Activity.java:6389)
      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3158)
      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3247) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5582) 

This error only occurs if second activity is opened from onActivityResult.

Edited:

getpath method

public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        cursor.close();
        return path;
    }
    // this is our fallback here
    return uri.getPath();
}

How can I solve this error?

user1590595
  • 795
  • 2
  • 13
  • 37
  • 2
    "which is actually working fine" -- probably not. Delete `getPath()`, as there is no valid implementation of such a method. "I gets following error, when back button is pressed" -- if I had to guess, you are using the `managedQuery()` method, which has been deprecated for six years. You might want to edit your question and show the code where you are obtaining your `Cursor`. – CommonsWare May 19 '17 at 11:47
  • I have added the getpath method. – user1590595 May 19 '17 at 12:02
  • Got it resolved by using solution from http://stackoverflow.com/questions/2789276/android-get-real-path-by-uri-getpath Thanks for help – user1590595 May 19 '17 at 12:04

1 Answers1

3

First, managedQuery() has been deprecated for over six years. Do not use it. Use a CursorLoader, or use ContentResolver and query() on some form of background thread.

Second, be sure to follow the instructions of the things that you use. In this case, you missed the instructions for using managedQuery():

Warning: Do not call close() on a cursor obtained using this method, because the activity will do that for you at the appropriate time.

Beyond that, a Uri does not point to a file in most cases, and so you cannot get a "real path" to that file.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491