无法从 Android 10 中的意图打开文件
Can't open file from intent in Android 10
我试图从 Intent.ACTION_GET_CONTENT 访问一个文件,当我在我的设备(Android 8)上尝试时,它工作得很好。但是,当我在我朋友的设备 (Android 10) 上尝试时,它不起作用。当我尝试从 Word 打开文件时,它一直提示“无法打开文件。请尝试将文件保存在设备上,然后再打开它。”当我打开一个pdf文件时,它什么也没有显示,只是黑屏。
btn_add OnClick 侦听器
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] mimeTypes = {"application/vnd.google-apps.document", "application/pdf", "application/vnd.google-apps.form",
"application/vnd.google-apps.presentation", "application/vnd.google-apps.spreadsheet",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/x-excel"};
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
intent.setType(mimeTypesStr.substring(0, mimeTypesStr.length() - 1));
} startActivityForResult(intent, 100);
}
});
onActivityResult
titleArrays = new ArrayList<>();
ItemAdapter adapter = new ItemAdapter(titleArrays);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
Log.d("Id: ", ""+id);
hashMap.put(id, data.getData());
returnCursor =
getContentResolver().query(data.getData(), null, null, null, null);
nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
titleHashmap.put(id, returnCursor.getString(nameIndex));
for (int i : hashMap.keySet()){
titleArrays.add(new ItemProperty(hashMap.get(i), titleHashmap.get(i)));
}
img_file.setImageResource(0);
id++;
// Item OnClick
adapter.setOnItemClickListener(new ItemAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Log.d("Position: ", ""+position);
Intent intent = new Intent(Intent.ACTION_VIEW, titleArrays.get(position).getUri());
startActivity(intent);
}
});
我想知道我做错了什么。如果你们知道,请告诉我。谢谢!
所以,我终于按照 CommonsWare 说明解决了这个问题!我添加了 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
到我的 Action_VIEW
Intent
并将我的 ACTION_GET_CONTENT
更改为 ACTION_OPEN_DOCUMENT
btn_add OnClick 侦听器
String[] mimeTypes = {"application/vnd.google-apps.document", "application/pdf", "application/vnd.google-apps.form",
"application/vnd.google-apps.presentation", "application/vnd.google-apps.spreadsheet",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/x-excel"};
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
intent.setType(mimeTypesStr.substring(0, mimeTypesStr.length() - 1));
}
startActivityForResult(intent, 100);
onActivityResult
if (resultCode == RESULT_OK) {
titleArrays = new ArrayList<>();
ItemAdapter adapter = new ItemAdapter(titleArrays);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
Log.d("Id: ", ""+id);
hashMap.put(id, data.getData());
returnCursor =
getContentResolver().query(data.getData(), null, null, null, null);
nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
titleHashmap.put(id, returnCursor.getString(nameIndex));
for (int i : hashMap.keySet()){
titleArrays.add(new ItemProperty(hashMap.get(i), titleHashmap.get(i)));
}
img_file.setImageResource(0);
id++;
// Item OnClick
adapter.setOnItemClickListener(new ItemAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Log.d("Position: ", ""+position);
Intent intent = new Intent(Intent.ACTION_VIEW, titleArrays.get(position).getUri());
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(intent);
}
});
我试图从 Intent.ACTION_GET_CONTENT 访问一个文件,当我在我的设备(Android 8)上尝试时,它工作得很好。但是,当我在我朋友的设备 (Android 10) 上尝试时,它不起作用。当我尝试从 Word 打开文件时,它一直提示“无法打开文件。请尝试将文件保存在设备上,然后再打开它。”当我打开一个pdf文件时,它什么也没有显示,只是黑屏。
btn_add OnClick 侦听器
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] mimeTypes = {"application/vnd.google-apps.document", "application/pdf", "application/vnd.google-apps.form",
"application/vnd.google-apps.presentation", "application/vnd.google-apps.spreadsheet",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/x-excel"};
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
intent.setType(mimeTypesStr.substring(0, mimeTypesStr.length() - 1));
} startActivityForResult(intent, 100);
}
});
onActivityResult
titleArrays = new ArrayList<>();
ItemAdapter adapter = new ItemAdapter(titleArrays);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
Log.d("Id: ", ""+id);
hashMap.put(id, data.getData());
returnCursor =
getContentResolver().query(data.getData(), null, null, null, null);
nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
titleHashmap.put(id, returnCursor.getString(nameIndex));
for (int i : hashMap.keySet()){
titleArrays.add(new ItemProperty(hashMap.get(i), titleHashmap.get(i)));
}
img_file.setImageResource(0);
id++;
// Item OnClick
adapter.setOnItemClickListener(new ItemAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Log.d("Position: ", ""+position);
Intent intent = new Intent(Intent.ACTION_VIEW, titleArrays.get(position).getUri());
startActivity(intent);
}
});
我想知道我做错了什么。如果你们知道,请告诉我。谢谢!
所以,我终于按照 CommonsWare 说明解决了这个问题!我添加了 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
到我的 Action_VIEW
Intent
并将我的 ACTION_GET_CONTENT
更改为 ACTION_OPEN_DOCUMENT
btn_add OnClick 侦听器
String[] mimeTypes = {"application/vnd.google-apps.document", "application/pdf", "application/vnd.google-apps.form",
"application/vnd.google-apps.presentation", "application/vnd.google-apps.spreadsheet",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/x-excel"};
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
intent.setType(mimeTypesStr.substring(0, mimeTypesStr.length() - 1));
}
startActivityForResult(intent, 100);
onActivityResult
if (resultCode == RESULT_OK) {
titleArrays = new ArrayList<>();
ItemAdapter adapter = new ItemAdapter(titleArrays);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
Log.d("Id: ", ""+id);
hashMap.put(id, data.getData());
returnCursor =
getContentResolver().query(data.getData(), null, null, null, null);
nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
titleHashmap.put(id, returnCursor.getString(nameIndex));
for (int i : hashMap.keySet()){
titleArrays.add(new ItemProperty(hashMap.get(i), titleHashmap.get(i)));
}
img_file.setImageResource(0);
id++;
// Item OnClick
adapter.setOnItemClickListener(new ItemAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Log.d("Position: ", ""+position);
Intent intent = new Intent(Intent.ACTION_VIEW, titleArrays.get(position).getUri());
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(intent);
}
});