0

I would like to implement a Picture Chooser, since there's a bug with the Recent image foler and the Download Folder the Android app crashes when selecting an image from these two folders, I would like to hide them, or open directly the Gallery folder without showing the Folder chooser panel,

This is my actual code :

Intent intent = new Intent (Intent.ACTION_GET_CONTENT);
startActivityForResult (intent, 1000);

and in the on Result Method :

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent intent) {

    super.onActivityResult (requestCode, resultCode, intent);

    String uri = intent.getDataString ();
    String absolutePath = getRealPathFromURI (uri);
    String compressedFilePath = compressImage (absolutePath);

    encodedResult = base64File (new File (compressedFilePath)); //String
}

protected String getRealPathFromURI (String contentURI, UIView uiView) {
    Uri contentUri = Uri.parse(contentURI);
    Cursor cursor = ((ContextWrapper) uiView).getContentResolver ().query(contentUri, null, null, null, null);
    if (cursor == null) {
        return contentUri.getPath();
    } else {
        cursor.moveToFirst();
        int index = cursor.getColumnIndex(MediaColumns.DATA);
        return cursor.getString(index);
    }
}

So when I select an image from a folder like : Gallery, Pictures etc... It works just fine. But for the 2 folders : Download and Recent it crashes and gives me :

java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

Or maybe there is a more correct way of doing that which I'm not aware of it ?

Do you have any idea about this ? How can I achieve that ? Thanks.

  • 2
    Your Android app crashes? Why does that happen? – ianhanniballake Apr 02 '14 at 17:54
  • 1
    Agreed. Rather than reducing functionality for your user -- and in ways that will be incompatible across devices -- I would focus on getting help for your existing problems. – CommonsWare Apr 02 '14 at 17:58
  • Thank you. And yes I agree, this is wiser. I have updated my question with further details. –  Apr 02 '14 at 18:12

1 Answers1

0

it crashes and gives me

That is because you are assuming that any Uri can be converted into a File. That has never been supported.

Use a ContentResolver and consume the Uri via openInputStream() (or openOutputStream(), if appropriate).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you. I didn't know that. I'll digg in that direction. :) –  Apr 02 '14 at 19:55
  • @CommonsWare can you please help me here i did posted a question:http://stackoverflow.com/questions/37883092/android-action-get-content-does-not-update-download-dir-files – Pans Jun 17 '16 at 14:11