gpt4 book ai didi

Android-从 URI 获取文件路径?

转载 作者:太空宇宙 更新时间:2023-11-03 11:26:08 25 4
gpt4 key购买 nike

我需要获取文件的路径以将其转换为输入流以便上传。所以我使用文件选择器选择 pdf 文档。我在 onActivityResult() 中得到 Uri。使用 uri ,我得到文件的路径。但它确实不返回正确的路径。绝对路径:

when choosing file from internal storage 
/document/primary:pdf/HA License Plate.pdf

when choosing file from external storage
/document/9016-4EF8:certificates/img002.pdf

所以我无法获得正确的文件路径,无法转换为输入流。所以请建议我在我的 android 应用程序中获取正确的文件路径?我使用的代码如下。

代码是:-

public void ChoosePDF(View v) {
Intent intent = new Intent();
// intent.setType("pdf/*");
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"),
REQUEST_CODE);
}

public void onActivityResult(int requestCode, int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE) {
Uri data = result.getData();


if (data.getLastPathSegment().endsWith("pdf")) {
String pdfPath = data.getPath();
File myFile = new File(data.getPath());

Log.i("FirstActivity", "pdfPath is " + pdfPath);
Log.i("FirstActivity", "uri abs path is " + myFile.getAbsolutePath());

} else {
Toast.makeText(SplashScreenActivity.this,
"Invalid file type.Please choose valid file!",
Toast.LENGTH_LONG).show();
}
}
}
}

获取文件路径时是否存在任何特定于 android 版本的问题?我正在使用 android lollipop 设备进行测试?

最佳答案

使用文件 file=new File(new URI(data.toString()));

关于Android-从 URI 获取文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31902487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com