从文件存储中选择文件后如何从文件选择器中获取文件路径

时间:2019-10-03 12:40:46

标签: java android storage

我正在尝试创建一个带有按钮的个人聊天应用程序。当单击该按钮时,将显示框架,其中单击选择器上的按钮是这样创建的。单击图像按钮后,它会在选择我将其保存在图像文件Uri中的文件后创建选择器活动。

但是,当我尝试使用data.getdata().getpath()方法获取文件的路径时,它将给出doument/236作为输出,但没有给出文件的实际路径。当我尝试使用fileutlis获取路径时,它说“无法解析fileutils”。请帮助我,以便我可以获取文件的路径。

imagesend.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        checker = "image";
        Intent imageIntent = new Intent();
        imageIntent.setAction(Intent.ACTION_GET_CONTENT);
        imageIntent.setType("image/*");
        startActivityForResult(Intent.createChooser(imageIntent,"Select Image"),438);
    }
});

 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 438 && resultCode == RESULT_OK && data!=null && data.getData()!=null){
        loadingBar.setTitle("Sending Message");
        loadingBar.setMessage("Please wait...");
        loadingBar.setCanceledOnTouchOutside(false);
        loadingBar.show();

        imagefile = data.getData();

        String filepath = data.getData().getPath();

        if(checker.equals("pdf")){
            pdfFilemessage();
        }else if(checker.equals("image")){
            //imagefilemessage();
            Toast.makeText(personalChat.this,filepath,Toast.LENGTH_SHORT).show();
        }
    }
}

3 个答案:

答案 0 :(得分:2)

此行返回文件的Uri。

imagefile = data.getData();

您要做的是,

  public String getRealPathFromURI(Uri contentUri) {
        String[] proj = {
            MediaStore.Audio.Media.DATA
        };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

将此添加到您的代码中。

String filePath = this.getRealPathFromURI(imagefile);

请参阅:https://stackoverflow.com/a/13209514

答案 1 :(得分:0)

您可以尝试以下方法:

jtc

现在,您可以使用下面的要点文件从URI生成文件路径

https://gist.github.com/tatocaster/32aad15f6e0c50311626

答案 2 :(得分:0)

在Android 10上,默认情况下您将无法直接通过路径访问文件,因为Google强迫人们使用Storage Access Framework和/或MediaStore来访问文件。

对于Android 11,他们的计划是使它成为访问文件的唯一方法。

有关详细信息,请参见https://developer.android.com/training/data-storage/files/external-scoped

您将需要使用ContentResolver#openFileDescriptor(Uri, String)ParcelFileDescriptor#getFileDescriptor()以获得可用的东西

例如

ParcelFileDescriptor pfd =
                    this.getContentResolver().
                            openFileDescriptor(Uri, "r");

            FileInputStream fileInputStream =
                    new FileInputStream(
                            pfd.getFileDescriptor());


如果要获取有关该文件的其他信息,可以使用DISPLAY_NAME或MIME_TYPE上URI上的contentResolver查询来实现。

例如

// useful name to display to user
  Cursor cursor = this.getContentResolver()
    .query(Uri, new String[] { MediaStore.Files.FileColumns.DISPLAY_NAME},
    null, null, null);

    cursor.moveToFirst();
    filename = cursor.getString(0);
    cursor.close();