• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Cannot get actual file path from downloads category of File Chooser

I have created an Android Media Player and I have run into a dilemma. I have created a file picker for the mediaplayer but the File chooser intent built into android does not allow me to get actual file path in such format "/storage/emulated/0/etc./" instead it returns the URI path to the file being played. I have created a function to get the real file path for files chosen through file chooser intent which is list below. The short of it is that any file chosen from the recent tab or downloads section of the File Chooser intent it will not get the real file path for any file chosen from those categories within the File Chooser. Is there any way to get the real file path for files chosen from the File Chooser instead of URI's?

Code:
 public static String removableStoragePath;
    public static String sdPath, sdCardLabel;
    public static boolean sdCardPathDetection;
    public static int dirCounterSD;
    public static ArrayList<Integer> iterationCounterLocationsSD = new ArrayList<Integer>();

    @SuppressLint("NewApi")
    public static String getPath(Context context, Uri uri) {
        final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19;
        String selection = null;
        System.out.println("URI SCHEME: " + uri.getScheme() + "getDocumentId: " + DocumentsContract.getDocumentId(uri));
        String[] selectionArgs = null;
        // Uri is different in versions after KITKAT (Android 4.4), we need to
        // deal with different Uris.
        if (DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];
                File fileList[] = new File("/storage/").listFiles();
                for (File file : fileList) {
                    if (!file.toString().contains("self") && !file.toString().contains("emulated")) {
                        sdPath = file.toString();
                        System.out.println("SD PATH SUCCESSFULLY IDENTIFIED!: " + sdPath);
                    }
                    sdCheck(uri);
                }
                System.out.println(removableStoragePath);
                return removableStoragePath + "/" + split[1];

            } else {
                if (isMediaDocument(uri)) {
                    String pathUtil = uri.getPath();
                    System.out.println("Media document: " + pathUtil);
                    final String docId = DocumentsContract.getDocumentId(uri);
                    final String[] split = docId.split(":");
                    File fileList[] = new File("/storage/").listFiles();
                    for (File file : fileList) {
                        if (file.toString().contains("self") || file.toString().contains("emulated")) {
                            sdPath = file.toString();
                            System.out.println("SD PATH SUCCESSFULLY IDENTIFIED!: " + sdPath);
                        }
                        sdCheck(uri);
                    }
                    System.out.println(removableStoragePath);
                    return removableStoragePath + "/" + split[1];
                }
            }
        }
        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = {MediaStore.Images.Media.DATA};
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
            }
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        return null;
    }

    public static void sdCheck(Uri uri) {
        System.out.println("SD CHECK IS WORKING!");
        for (int i = 0; i < uri.toString().length(); i++) {
            char c = uri.toString().charAt(i);
            //Process char
            if (c == '/') {
                dirCounterSD += 1;
                iterationCounterLocationsSD.add(i);
            }
        }
        Object iterationArraySD[] = iterationCounterLocationsSD.toArray();
        int lastIndexPathSD = iterationArraySD.length;
        int lastIndexSlashSD = (int) iterationArraySD[lastIndexPathSD - 1];
        System.out.println("# Slashes: " + lastIndexPathSD + " Slash index: "
                + iterationArraySD[lastIndexPathSD - 1].toString());

        sdCardLabel = uri.toString().substring(lastIndexSlashSD + 1, lastIndexSlashSD + 10);
        if (sdCardLabel.toCharArray()[4] == "-".toCharArray()[0]) {
            sdCardPathDetection = true;
            removableStoragePath = sdPath;
        } else {
            removableStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        System.out.println("SD Card Label: " + sdCardLabel);

    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones