我有一个文件的路径,我希望设备用另一个应用程序打开文件
public static void openUrl(Context context, String path) {
Uri uriForFile;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
uriForFile = GenericFileProvider.getUriForFile(context,"xyz.provider",new File(path));
}else{
uriForFile = Uri.fromFile(new File(path));
}
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(fileExt(path).substring(1));
newIntent.setDataAndType(uriForFile, mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(newIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, R.string.msg_noHandlerForThisTypeOfFile, Toast.LENGTH_LONG).show();
}
}
上面的代码适用于api 23及更低版本
但是在android 7中我想打开一个mp3文件,例如Toast“播放器不支持这种类型的音频文件”
或当我打开图像时,它会打开一个带有图库应用的空白页面。
答案 0 :(得分:2)
替换:
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
使用:
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION);
目前,第三方应用对您的GenericFileProvider
内容没有任何权利。