onActivityResult's intent.getPath() doesn't give me the correct filename

后端 未结 2 570
清酒与你
清酒与你 2020-11-22 00:56

I am trying to fetch a file this way:

final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
    String[] mimetypes = {\"application/pdf\"};
         


        
2条回答
  •  难免孤独
    2020-11-22 01:29

    To get the real name and to avoid getting a name that looks like "image: 4431" or even just a number, you can write code as recommended by CommonsWare.
    The following is an example of a code that selects a single pdf file, prints its name and path to the log, and then sends the file by email using its uri.

    private static final int FILEPICKER_RESULT_CODE = 1;
    private static final int SEND_EMAIL_RESULT_CODE = 2;
    private Uri fileUri;
    
    private void chooseFile() {
        Intent fileChooser = new Intent(Intent.ACTION_GET_CONTENT);
        fileChooser.setType("application/pdf");
        startActivityForResult(Intent.createChooser(fileChooser, "Choose one pdf file"), FILEPICKER_RESULT_CODE);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == FILEPICKER_RESULT_CODE) {
            if (resultCode == RESULT_OK) {
                fileUri = data != null ? data.getData() : null;
                if (fileUri != null) {
                    DocumentFile d = DocumentFile.fromSingleUri(this, fileUri);
                    if (d != null) {
                        Log.d("TAG", "file name: " + d.getName());
                        Log.d("TAG", "file path: " + d.getUri().getPath());
                        sendEmail(fileUri);
                    }
                }
            }
        }
    }
    
    private void sendEmail(Uri path) {
        String email = "example@gmail.com";
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("application/octet-stream");
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "PDF file");
        String[] to = { email };
        intent.putExtra(Intent.EXTRA_EMAIL, to);
        intent.putExtra(Intent.EXTRA_TEXT, "This is the pdf file...");
        intent.putExtra(Intent.EXTRA_STREAM, path);
        startActivityForResult(Intent.createChooser(intent, "Send mail..."), SEND_EMAIL_RESULT_CODE);
    }
    

    hope it helps.

提交回复
热议问题