我的问题是,当我尝试显示用户通过文件选择器通过以下代码选择的PDF文件时,它可以正常工作,并且PDF文件打开,但是当我使用以下命令存储URI(selectedfile)时file.setUri(selectedfile)
来自用户选择的文件,并在另一个活动中尝试使用相同的代码(相同的Intent等),然后打开PDF文件,但没有显示内容(显示黑屏)。怎么了我使用的是完全相同的URI(selectedfile)和相同的Intent命令。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 123 && resultCode == RESULT_OK) {
selectedfile = data.getData(); //The uri with the location of the file
String path=getPathFromUri(this,selectedfile);
CFile file= new CFile(path,"test","",true);
file.setUri(selectedfile);
professor.UploadFile(file,course);
DisplayCourseP.adapter.notifyDataSetChanged();
Intent intent= new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(file.getUri(),"*/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
finish();