我使用以下方式从本地存档中选择PDF文件:
startActivityForResult(Intent.createChooser(new Intent().setType("application/pdf").setAction(Intent.ACTION_GET_CONTENT), getString(R.string.str_select_file)), request_id_archive);
并管理结果:
@Override
protected void onActivityResult(int request_code, int result_code, Intent data) {
super.onActivityResult(request_code, result_code, data);
if (request_code == request_id_archive && result_code == RESULT_OK && data != null && data.getData() != null) {
Uri myuri = data.getData();
String mypath = myuri.getPath();
File myfile = new File(mypath);
Log.d("mylog1", uri.toString());
Log.d("mylog2", uri.getPath());
Log.d("mylog3", f.getPath());
Log.d("mylog4", f.toString());
Log.d("mylog5", Boolean.toString(f.exists()));
}
else {...}
似乎文件创建不成功。 我的日志结果如下:
- mylog1 -> content://media/external/file/7406
- mylog2 -> /external/file/7406
- mylog3 -> /external/file/7406
- mylog4 -> /external/file/7406
- mylog5 -> false
在其他代码中,我尝试对该文件进行多种操作。
...
InputStream inputStream = new FileInputStream(file);
...
但是我的应用程序崩溃了,在Logcat中我看到了以下信息:
java.io.FileNotFoundException: /document/raw:/storage/emulated/0/Download/20180702_121938.pdf (No such file or directory)
我正在Android 7上测试我的代码。
Intent intentPDF = new Intent(Intent.ACTION_GET_CONTENT); intentPDF.setType("application/pdf"); intentPDF.addCategory(Intent.CATEGORY_OPENABLE);
- Hemant Parmardocument/raw:/storage/emulated/0/Download/20180702_121938.pdf
不是一个有效的路径。请尝试使用/storage/emulated/0/Download/20180702_121938.pdf
。 - greenapps