Extract file names from SAF content URIs

Hi my colleagues, stackoverflows,

I am writing an application in which I implemented an Activity that handles general intentions. While this works fine, but during testing I have a problem with Quickoffice (Android 4.4, KitKat), because it returns a URI from which I cannot get the file name. I also tried sharing with other apps like Dropbox, and it works there.

The exact URI I get from the Qickoffice app:

content://com.quickoffice.android.quickcommon.FileContentProvider/5cmeDeeatcdv8IFyu-bEr2w1jSHrvPmCzXGb_VvZulMBErE5Tmfd_5P5kckE68LaEYDVSp3q5r19%0A4sOkpYCEM_VqK6Y%3D%0A

This was the first code I used:

public String getRealPathFromURI(Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA}; // = "_data"
        ContentResolver cr = getContentResolver(); 
        cursor = cr.query(contentUri, proj, null, null, null); // <--EXCEPTION

        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } catch (Exception exception) {
        Log.d("clixend", "Exception: " + exception);
        Toast.makeText(this, "Exception: " + exception, Toast.LENGTH_LONG).show();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

Where I got the following error:

09-23 16:54:17.664  32331-32341/? E/DatabaseUtils﹕ Writing exception to parcel
java.lang.UnsupportedOperationException: Unsupported column: _data
        at com.google.android.apps.docs.quickoffice.FileContentProvider.query(FileContentProvider.java:78)
        at android.content.ContentProvider.query(ContentProvider.java:857)
        at android.content.ContentProvider$Transport.query(ContentProvider.java:200)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
        at android.os.Binder.execTransact(Binder.java:404)
        at dalvik.system.NativeStart.run(Native Method)

, Android 4.4 Kitkat SAF (Storage Access Framework), -, https://developer.android.com/guide/topics/providers/document-provider.html, :

public String getNameKitkat(Uri contentUri) {
Cursor cursor = getContentResolver()
            .query(contentUri, null, null, null, null, null); // <--EXCEPTION
    try {
        if (cursor != null && cursor.moveToFirst()) {
            String displayName = cursor.getString(
                    cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

           return displayName;
        }
    } finally {
        cursor.close();
    }
    return null;
}

:

09-23 16:49:43.317  32331-32421/? E/DatabaseUtils﹕ Writing exception to parcel
java.lang.IllegalArgumentException: columnNames.length = 4, columnValues.size() = 2
        at android.database.MatrixCursor.addRow(MatrixCursor.java:157)
        at android.database.MatrixCursor.addRow(MatrixCursor.java:128)
        at com.google.android.apps.docs.quickoffice.FileContentProvider.query(FileContentProvider.java:95)
        at android.content.ContentProvider.query(ContentProvider.java:857)
        at android.content.ContentProvider$Transport.query(ContentProvider.java:200)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
        at android.os.Binder.execTransact(Binder.java:404)
        at dalvik.system.NativeStart.run(Native Method)

- , URI, Quickoffice, .

+4
1

, , URI, Quickoffice.

public String getNameFromURI(Uri contenturi){

    String[] proj = {
            OpenableColumns.DISPLAY_NAME,
            OpenableColumns.SIZE
    };
    String name = null;
    int size= 0;
    Cursor metadataCursor = getContentResolver().query(contenturi,  proj, null, null, null);

    if (metadataCursor != null) {
        try {
            if (metadataCursor.moveToFirst()) {
                name = metadataCursor.getString(0);
                size = metadataCursor.getInt(1);
            }
        } finally {
            metadataCursor.close();
        }
    }

    return name;
}

, Typ, . :

String[] proj = {
        OpenableColumns.DISPLAY_NAME,
        OpenableColumns.SIZE
};

. .

+5

All Articles