Convert file uri to content uri

前端 未结 5 601
暗喜
暗喜 2020-12-06 05:44

i want to choose a file (via file chooser) from astro manager (in my case a *.pdf or *.doc) but the uri contains only the file path (\"sdcard/my_folder/test.pdf\"

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 06:11

    One simple way to achieve this could be by using MediaScannerConnection

    File file = new File("pathname");
    MediaScannerConnection.scanFile(getContext(), new String[]{file.getAbsolutePath()}, null /*mimeTypes*/, new MediaScannerConnection.OnScanCompletedListener() {
                @Override
                public void onScanCompleted(String s, Uri uri) {
                    // uri is in format content://...
                }
            });
    

提交回复
热议问题