如何在 Android Studio 中提取本地文件的位置?

问题描述

我正在尝试构建一个程序,我需要允许用户浏览手机中的特定文件并选择一个文件。我目前正在尝试从用户的手机中提取所选文件的位置,但我不知道如何做到这一点。 任何人都可以建议我如何做到这一点,尤其是我可以在布局文件中使用什么工具以及如何提取主类中的位置?

解决方法

首先我们必须在点击按钮时开始意图 即

private void openFileManager(int  CODE) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    startActivityForResult(intent,CODE);
}

我们可以使用

在 onActivityResult 中获取路径
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
    String FilePath = data.getDataString();

    //Whatever you want to do .....You can do here

    super.onActivityResult(requestCode,resultCode,data);
}