How to get the file path from uri in Android devices having Oreo and above OS

前端 未结 3 1394
长发绾君心
长发绾君心 2021-01-16 21:34

To select an audio file using Intent , I am using this function :-

fun selectAudioFromStorage() {
    val pictureActionIntent = Intent(Intent.ACTION_GET_CONT         


        
3条回答
  •  孤街浪徒
    2021-01-16 22:12

    As file paths are not usable in API 29 and above for public files I suggest that you don't try to use them but use a Java FileDescriptor instead.

    As per https://developer.android.com/training/data-storage#scoped-storage

    And more specifically https://developer.android.com/training/data-storage/shared/media#open-file-descriptor

    A lot of API's allow you to provide a FileDescriptor instead of a path.

    e.g. Use https://developer.android.com/reference/android/media/MediaPlayer.html#setDataSource(java.io.FileDescriptor)

    instead of

    https://developer.android.com/reference/android/media/MediaPlayer.html#setDataSource(java.lang.String)

    To get a fileDescriptor from a URI do the following (in Java)

    
    ParcelFileDescriptor pfd =
                    this.getContentResolver().
                            openFileDescriptor(URI, "r");
    
    FileDescriptor fileDescriptor = pfd.getFileDescriptor();
    
    

提交回复
热议问题