0

The code below uploads a file for an user. How can I fix it? I selected a file from my drive and data.getData() is not null, but get real path does not work:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        Uri uri = data.getData();
        Log.i("errorCheck","L: "+getPath(this, uri));
    }
}

public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    public static String getDataColumn(Context context, Uri uri, String selection,
                                       String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }

    public static String getPath(final Context context, final Uri uri) {

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
        Log.i("errorCheck", "KIT: "+isKitKat);
        // DocumentProvider
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (DocumentsContract.isDocumentUri(context, uri)) {
                // ExternalStorageProvider
                if (isExternalStorageDocument(uri)) {
                    final String docId = DocumentsContract.getDocumentId(uri);
                    final String[] split = docId.split(":");
                    final String type = split[0];

                    if ("primary".equalsIgnoreCase(type)) {
                        return Environment.getExternalStorageDirectory() + "/" + split[1];
                    }

                    // TODO handle non-primary volumes
                }
                // DownloadsProvider
                else if (isDownloadsDocument(uri)) {

                    final String id = DocumentsContract.getDocumentId(uri);
                    final Uri contentUri = ContentUris.withAppendedId(
                            Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                    return getDataColumn(context, contentUri, null, null);
                }
                // MediaProvider
                else if (isMediaDocument(uri)) {
                    final String docId = DocumentsContract.getDocumentId(uri);
                    final String[] split = docId.split(":");
                    final String type = split[0];

                    Uri contentUri = null;
                    if ("image".equals(type)) {
                        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    } else if ("video".equals(type)) {
                        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                    } else if ("audio".equals(type)) {
                        contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                    }

                    final String selection = "_id=?";
                    final String[] selectionArgs = new String[] {
                            split[1]
                    };

                    return getDataColumn(context, contentUri, selection, selectionArgs);
                }
            }
        } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(context, uri, null, null);
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

But it always returns null.

My mobile android version : 4.3

Mobile : Galaxy S3

Daniel
  • 2,355
  • 9
  • 23
  • 30
  • https://stackoverflow.com/questions/10386885/intent-filter-intent-getdata-returns-null&ved=2ahUKEwjFjLm448neAhVEecAKHfmxA7cQjjgwAXoECAUQAQ&usg=AOvVaw1joiPEHROGd8thPEDkkc3L – teh_raab Nov 10 '18 at 12:06
  • See [this](https://stackoverflow.com/questions/49221312/android-get-real-path-of-a-txt-file-selected-from-the-file-explorer) and [this](https://stackoverflow.com/questions/48510584/onactivityresults-intent-getpath-doesnt-give-me-the-correct-filename) and [this](https://stackoverflow.com/questions/35870825/getting-the-absolute-file-path-from-content-uri-for-searched-images). – CommonsWare Nov 10 '18 at 12:08
  • @teh_raab this code return null – soltan world Nov 10 '18 at 12:13
  • @CommonsWare i saw them, but none of them not work – soltan world Nov 10 '18 at 12:14
  • 1
    Perhaps you should write a Stack Overflow question where you provide a [mcve] showing what you tried with respect using `openInputStream()` to work with a `Uri`, explaining how you have demonstrated that "none of them work" and asking for help. Or, if you only want to work with files, [use a file chooser library](https://android-arsenal.com/tag/35?sort=created). – CommonsWare Nov 10 '18 at 12:18
  • I would reiterate @CommonsWare comments. Update your question. Show full example.. we cannot see where/how you're sending the Intent. There are tons of examples of how to get this working on here alone. – teh_raab Nov 10 '18 at 12:23
  • try using 'RealPathUtil' given in this link : https://gist.github.com/tatocaster/32aad15f6e0c50311626 – Rahul Sonpaliya Nov 10 '18 at 12:35
  • @RahulSonpaliya: That will fail for many `Uri` values and for many devices. It is much the same as the code in the question, which also will fail for many `Uri` values and many devices. There is no requirement for a `Uri` to point to a file on the filesystem, let alone a file that you can access, let alone a file that you can derive its path from the `Uri`. – CommonsWare Nov 10 '18 at 12:43

0 Answers0