Android - 如何从文档中获取选定的文件名

And*_*Dev 21 android android-intent android-file

我正在使用以下代码启动选择documnets的意图.

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"), 1);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Please install a File Manager.",
                Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

在onActivity结果当我试图获取文件路径时,它在文件名的位置给出了一些其他数字.

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 1:
        if (resultCode == RESULT_OK) {
            // Get the Uri of the selected file
            Uri uri = data.getData();
            File myFile = new File(uri.toString());
            String path = myFile.getAbsolutePath();
        }
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
Run Code Online (Sandbox Code Playgroud)

这条道路的价值我是这样的."content://com.android.providers.downloads.documents/document/1433"但我想要像doc1.pdf等真正的文件名.如何获得它?

小智 48

当您获得内容:// uri时,您需要查询内容解析器,然后获取显示名称.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 1:
        if (resultCode == RESULT_OK) {
            // Get the Uri of the selected file
            Uri uri = data.getData();
            String uriString = uri.toString();
            File myFile = new File(uriString);
            String path = myFile.getAbsolutePath();
            String displayName = null;

            if (uriString.startsWith("content://")) {                   
                Cursor cursor = null;
                try {                           
                    cursor = getActivity().getContentResolver().query(uri, null, null, null, null);                         
                    if (cursor != null && cursor.moveToFirst()) {                               
                        displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                    }
                } finally {
                    cursor.close();
                }
            } else if (uriString.startsWith("file://")) {           
                displayName = myFile.getName();
            }
        }
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
Run Code Online (Sandbox Code Playgroud)

  • 如何获取文件路径?你只获得文件名 (3认同)
  • @cammanod我最近经历了一个新问题,以获取Android N中的文件路径和[File Path Finder](https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on -android-nougat/en)链接帮助了我.试试这个,让我知道它是否对你有帮助. (2认同)

Mad*_*ddy 0

你可以试试这个,希望对你有帮助:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("inside", "onActivityResult");
        if(requestCode == FILE_MANAGER_REQUEST_CODE)
        {
            // Check if the user actually selected an image:
            if(resultCode == Activity.RESULT_OK)
            {
                // This gets the URI of the image the user selected:
                Uri selectedFileURI = data.getData();
                File file = new File(getRealPathFromURI(selectedFileURI));



                // Create a new Intent to send to the next Activity:
            }
        }
    }


    private String getRealPathFromURI(Uri contentURI) {
            String result;
            Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
            if (cursor == null) { // Source is Dropbox or other similar local file path
                result = contentURI.getPath();
            } else { 
                cursor.moveToFirst(); 
                int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
                result = cursor.getString(idx);
                cursor.close();
            }
            return result;
        }
Run Code Online (Sandbox Code Playgroud)