Pergunta

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.

Foi útil?

Solução

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).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top