在Android7.0之前打開(kāi)第三方文件是很簡(jiǎn)單的事情,(如下方這樣可直接打開(kāi)),但是Android 7.0之后我們會(huì)發(fā)現(xiàn)程序會(huì)直接閃退
報(bào)錯(cuò)
android.os.FileUriExposedException:
file:///storage/emulated/0/updata.apk exposed beyond app through Intent.getData()
然后經(jīng)過(guò)網(wǎng)上一番查找,就會(huì)查到FileProvider的相關(guān)解決方案,什么修改manifest、provider什么之類的,然后我們?cè)讲仍缴睿筋^來(lái)什么問(wèn)題都沒(méi)有解決,無(wú)奈只好自己親手持刀操作了
解決方案
此方法適合用FileProvider仍沒(méi)有解決的同胞們,亦或者直接用此方法:比較流氓的方法,繞過(guò)7.0的文件權(quán)限檢查
這里以打開(kāi)text文本為例
try {
? ? ? Uri uri = null;
? ? ? if (Build.VERSION.SDK_INT >= 24) {
? ? ? ? ? StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
? ? ? ? ? StrictMode.setVmPolicy(builder.build());
? ? ? ? ? uri = Uri.fromFile(file1);
? ? ? } else {
? ? ? ? ? uri = Uri.fromFile(file1);
? ? ? ? }
? ? ? ? ? Intent intent = new Intent(Intent.ACTION_VIEW);
? ? ? ? ? intent.addCategory(Intent.CATEGORY_DEFAULT);
? ? ? ? ? intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
? ? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? ? intent.setDataAndType(uri, "text/plain");
? ? ? ? ? startActivity(intent);
? ? ? ? } catch (Exception e) {
? ? ? ? ? e.printStackTrace();
? ? ? }
6.0之前打開(kāi)文件方式
public class MyIntent {?
? ? //android獲取一個(gè)用于打開(kāi)HTML文件的intent?
? ? public static Intent getHtmlFileIntent( String param ) {?
? ? ? ? Uri uri = Uri.parse(param ).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build();?
? ? ? ? Intent intent = new Intent("android.intent.action.VIEW");?
? ? ? ? intent.setDataAndType(uri, "text/html");?
? ? ? ? return intent;?
? ? }?
? ? //android獲取一個(gè)用于打開(kāi)圖片文件的intent?
? ? public static Intent getImageFileIntent( String param ) {?
? ? ? ? Intent intent = new Intent("android.intent.action.VIEW");?
? ? ? ? intent.addCategory("android.intent.category.DEFAULT");?
? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);?
? ? ? ? Uri uri = Uri.fromFile(new File(param ));?
? ? ? ? intent.setDataAndType(uri, "image/*");?
? ? ? ? return intent;?
? ? }?
? ? //android獲取一個(gè)用于打開(kāi)PDF文件的intent?
? ? public static Intent getPdfFileIntent( String param ) {?
? ? ? ? Intent intent = new Intent("android.intent.action.VIEW");?
? ? ? ? intent.addCategory("android.intent.category.DEFAULT");?
? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);?
? ? ? ? Uri uri = Uri.fromFile(new File(param ));?
? ? ? ? intent.setDataAndType(uri, "application/pdf");?
? ? ? ? return intent;?
? ? }?
? ? //android獲取一個(gè)用于打開(kāi)文本文件的intent?
? ? public static Intent getTextFileIntent( String param, boolean paramBoolean) {?
? ? ? ? Intent intent = new Intent("android.intent.action.VIEW");?
? ? ? ? intent.addCategory("android.intent.category.DEFAULT");?
? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);?
? ? ? ? if (paramBoolean) {?
? ? ? ? ? ? Uri uri1 = Uri.parse(param );?
? ? ? ? ? ? intent.setDataAndType(uri1, "text/plain");?
? ? ? ? } else {?
? ? ? ? ? ? Uri uri2 = Uri.fromFile(new File(param ));?
? ? ? ? ? ? intent.setDataAndType(uri2, "text/plain");?
? ? ? ? }?
? ? ? ? return intent;?
? ? }?
? ? //android獲取一個(gè)用于打開(kāi)音頻文件的intent?
? ? public static Intent getAudioFileIntent( String param ) {?
? ? ? ? Intent intent = new Intent("android.intent.action.VIEW");?
? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);?
? ? ? ? intent.putExtra("oneshot", 0);?
? ? ? ? intent.putExtra("configchange", 0);?
? ? ? ? Uri uri = Uri.fromFile(new File(param ));?
? ? ? ? intent.setDataAndType(uri, "audio/*");?
? ? ? ? return intent;?
? ? }?
? ? //android獲取一個(gè)用于打開(kāi)視頻文件的intent?
? ? public static Intent getVideoFileIntent( String param ) {?
? ? ? ? Intent intent = new Intent("android.intent.action.VIEW");?
? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);?
? ? ? ? intent.putExtra("oneshot", 0);?
? ? ? ? intent.putExtra("configchange", 0);?
? ? ? ? Uri uri = Uri.fromFile(new File(param ));?
? ? ? ? intent.setDataAndType(uri, "video/*");?
? ? ? ? return intent;?
? ? }?
?
? ? //android獲取一個(gè)用于打開(kāi)CHM文件的intent?
? ? public static Intent getChmFileIntent( String param ) {?
? ? ? ? Intent intent = new Intent("android.intent.action.VIEW");?
? ? ? ? intent.addCategory("android.intent.category.DEFAULT");?
? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);?
? ? ? ? Uri uri = Uri.fromFile(new File(param ));?
? ? ? ? intent.setDataAndType(uri, "application/x-chm");?
? ? ? ? return intent;?
? ? }?
?
? ? //android獲取一個(gè)用于打開(kāi)Word文件的intent?
? ? public static Intent getWordFileIntent( String param ) {?
? ? ? ? Intent intent = new Intent("android.intent.action.VIEW");?
? ? ? ? intent.addCategory("android.intent.category.DEFAULT");?
? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);?
? ? ? ? Uri uri = Uri.fromFile(new File(param ));?
? ? ? ? intent.setDataAndType(uri, "application/msword");?
? ? ? ? return intent;?
? ? }?
? ? //android獲取一個(gè)用于打開(kāi)Excel文件的intent?
? ? public static Intent getExcelFileIntent( String param ) {?
? ? ? ? Intent intent = new Intent("android.intent.action.VIEW");?
? ? ? ? intent.addCategory("android.intent.category.DEFAULT");?
? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);?
? ? ? ? Uri uri = Uri.fromFile(new File(param ));?
? ? ? ? intent.setDataAndType(uri, "application/vnd.ms-excel");?
? ? ? ? return intent;?
? ? }?
? ? //android獲取一個(gè)用于打開(kāi)PPT文件的intent?
? ? public static Intent getPptFileIntent( String param ) {?
? ? ? ? Intent intent = new Intent("android.intent.action.VIEW");?
? ? ? ? intent.addCategory("android.intent.category.DEFAULT");?
? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);?
? ? ? ? Uri uri = Uri.fromFile(new File(param ));?
? ? ? ? intent.setDataAndType(uri, "application/vnd.ms-powerpoint");?
? ? ? ? return intent;?
? ? }?
}
作者:夢(mèng)乾
來(lái)源:簡(jiǎn)書