1. Are you ready for the Galaxy S20? Here is everything we know so far!

Cannot get actual file path from downloads category of File Chooser

Discussion in 'Android Development' started by James Mountain, May 17, 2020.

  1. James Mountain

    Thread Starter

    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 (Text):
    1.  public static String removableStoragePath;
    2.     public static String sdPath, sdCardLabel;
    3.     public static boolean sdCardPathDetection;
    4.     public static int dirCounterSD;
    5.     public static ArrayList<Integer> iterationCounterLocationsSD = new ArrayList<Integer>();
    6.  
    7.     @SuppressLint("NewApi")
    8.     public static String getPath(Context context, Uri uri) {
    9.         final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19;
    10.         String selection = null;
    11.         System.out.println("URI SCHEME: " + uri.getScheme() + "getDocumentId: " + DocumentsContract.getDocumentId(uri));
    12.         String[] selectionArgs = null;
    13.         // Uri is different in versions after KITKAT (Android 4.4), we need to
    14.         // deal with different Uris.
    15.         if (DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
    16.             if (isExternalStorageDocument(uri)) {
    17.                 final String docId = DocumentsContract.getDocumentId(uri);
    18.                 final String[] split = docId.split(":");
    19.                 final String type = split[0];
    20.                 File fileList[] = new File("/storage/").listFiles();
    21.                 for (File file : fileList) {
    22.                     if (!file.toString().contains("self") && !file.toString().contains("emulated")) {
    23.                         sdPath = file.toString();
    24.                         System.out.println("SD PATH SUCCESSFULLY IDENTIFIED!: " + sdPath);
    25.                     }
    26.                     sdCheck(uri);
    27.                 }
    28.                 System.out.println(removableStoragePath);
    29.                 return removableStoragePath + "/" + split[1];
    30.  
    31.             } else {
    32.                 if (isMediaDocument(uri)) {
    33.                     String pathUtil = uri.getPath();
    34.                     System.out.println("Media document: " + pathUtil);
    35.                     final String docId = DocumentsContract.getDocumentId(uri);
    36.                     final String[] split = docId.split(":");
    37.                     File fileList[] = new File("/storage/").listFiles();
    38.                     for (File file : fileList) {
    39.                         if (file.toString().contains("self") || file.toString().contains("emulated")) {
    40.                             sdPath = file.toString();
    41.                             System.out.println("SD PATH SUCCESSFULLY IDENTIFIED!: " + sdPath);
    42.                         }
    43.                         sdCheck(uri);
    44.                     }
    45.                     System.out.println(removableStoragePath);
    46.                     return removableStoragePath + "/" + split[1];
    47.                 }
    48.             }
    49.         }
    50.         if ("content".equalsIgnoreCase(uri.getScheme())) {
    51.             String[] projection = {MediaStore.Images.Media.DATA};
    52.             Cursor cursor = null;
    53.             try {
    54.                 cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
    55.                 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    56.                 if (cursor.moveToFirst()) {
    57.                     return cursor.getString(column_index);
    58.                 }
    59.             } catch (Exception e) {
    60.             }
    61.         } else if ("file".equalsIgnoreCase(uri.getScheme())) {
    62.             return uri.getPath();
    63.         }
    64.         return null;
    65.     }
    66.  
    67.     public static void sdCheck(Uri uri) {
    68.         System.out.println("SD CHECK IS WORKING!");
    69.         for (int i = 0; i < uri.toString().length(); i++) {
    70.             char c = uri.toString().charAt(i);
    71.             //Process char
    72.             if (c == '/') {
    73.                 dirCounterSD += 1;
    74.                 iterationCounterLocationsSD.add(i);
    75.             }
    76.         }
    77.         Object iterationArraySD[] = iterationCounterLocationsSD.toArray();
    78.         int lastIndexPathSD = iterationArraySD.length;
    79.         int lastIndexSlashSD = (int) iterationArraySD[lastIndexPathSD - 1];
    80.         System.out.println("# Slashes: " + lastIndexPathSD + " Slash index: "
    81.                 + iterationArraySD[lastIndexPathSD - 1].toString());
    82.  
    83.         sdCardLabel = uri.toString().substring(lastIndexSlashSD + 1, lastIndexSlashSD + 10);
    84.         if (sdCardLabel.toCharArray()[4] == "-".toCharArray()[0]) {
    85.             sdCardPathDetection = true;
    86.             removableStoragePath = sdPath;
    87.         } else {
    88.             removableStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    89.         }
    90.         System.out.println("SD Card Label: " + sdCardLabel);
    91.  
    92.     }
    93.  
    94.     /**
    95.      * @param uri The Uri to check.
    96.      * @return Whether the Uri authority is ExternalStorageProvider.
    97.      */
    98.     public static boolean isExternalStorageDocument(Uri uri) {
    99.         return "com.android.externalstorage.documents".equals(uri.getAuthority());
    100.     }
    101.  
    102.     /**
    103.      * @param uri The Uri to check.
    104.      * @return Whether the Uri authority is DownloadsProvider.
    105.      */
    106.     public static boolean isDownloadsDocument(Uri uri) {
    107.         return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    108.     }
    109.  
    110.     /**
    111.      * @param uri The Uri to check.
    112.      * @return Whether the Uri authority is MediaProvider.
    113.      */
    114.     public static boolean isMediaDocument(Uri uri) {
    115.         return "com.android.providers.media.documents".equals(uri.getAuthority());
    116.     }
    117. }
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Cannot actual file
  1. mikewax
    Replies:
    0
    Views:
    195
  2. unet
    Replies:
    0
    Views:
    306
  3. DridriLaBastos
    Replies:
    0
    Views:
    251
  4. kgash
    Replies:
    6
    Views:
    454
  5. ixoni
    Replies:
    8
    Views:
    719
  6. oneytlam
    Replies:
    2
    Views:
    592
  7. Christopher Clayton
    Replies:
    0
    Views:
    378
  8. robertanisoiu
    Replies:
    1
    Views:
    1,163
  9. etijskens
    Replies:
    3
    Views:
    543
  10. Provasek
    Replies:
    5
    Views:
    606

Share This Page

Loading...