Android 进入文件管理选中文件获取当前文件名

109 阅读1分钟

1.申请读写权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    tools:ignore="ScopedStorage" />

2.调用方法选择文件

private static final int PICK_PDF_REQUEST = 102;

private void selectPdfFile() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("application/pdf");// 设置文件类型为pdf
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, PICK_PDF_REQUEST);
}

文件类型整理:(根据所需要的类型选择下面格式)
image/jpeg
audio/mpeg4-generic
text/html
audio/mpeg
audio/aac
audio/wav
audio/ogg
audio/midi
audio/x-ms-wma
video/mp4
video/x-msvideo
video/x-ms-wmv
image/png
image/jpeg
image/gif
.xml ->text/xml
.txt -> text/plain
.cfg -> text/plain
.csv -> text/plain
.conf -> text/plain
.rc -> text/plain
.htm -> text/html
.html -> text/html
.pdf -> application/pdf
.apk -> application/vnd.android.package-archive
DOC = “application/msword”
DOCX = “application/vnd.openxmlformats-officedocument.wordprocessingml.document”
XLS = “application/vnd.ms-excel application/x-excel”
XLSX = “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”
PPT = “application/vnd.ms-powerpoint”
PPTX = “application/vnd.openxmlformats-officedocument.presentationml.presentation”

.doc application/msword
.dot application/msword

.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm application/vnd.ms-word.document.macroEnabled.12
.dotm application/vnd.ms-word.template.macroEnabled.12

.xls application/vnd.ms-excel
.xlt application/vnd.ms-excel
.xla application/vnd.ms-excel

.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm application/vnd.ms-excel.sheet.macroEnabled.12
.xltm application/vnd.ms-excel.template.macroEnabled.12
.xlam application/vnd.ms-excel.addin.macroEnabled.12
.xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12

.ppt application/vnd.ms-powerpoint
.pot application/vnd.ms-powerpoint
.pps application/vnd.ms-powerpoint
.ppa application/vnd.ms-powerpoint

.pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm application/vnd.ms-powerpoint.template.macroEnabled.12
.ppsm application/vnd.ms-powerpoint.slideshow.macroEnabled.12

.mdb application/vnd.ms-access

3.在activity里接受回调处理返回的url

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_PDF_REQUEST && resultCode == RESULT_OK) {
        if (data != null && data.getData() != null) {
            Uri uri = data.getData(); // 获取PDF文件的URI
            //根据Uri查询文件名
            Cursor cursor = ((Activity) context).getContentResolver().query(uri, null, null, null, null);
            if (cursor != null) {
                int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                cursor.moveToFirst();
                //文件名
                String fileName = cursor.getString(nameIndex);
                cursor.close();
                Log.i(TAG, "文件名:" + fileName + "---" + uri);
               }
          }
     }
}

效果展示↓ 图片.png