Android打開7.0軟件

在Android7.0之前打開第三方文件是很簡單的事情,(如下方這樣可直接打開),但是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)過網(wǎng)上一番查找,就會(huì)查到FileProvider的相關(guān)解決方案,什么修改manifest、provider什么之類的,然后我們越踩越深,到頭來什么問題都沒有解決,無奈只好自己親手持刀操作了

解決方案

此方法適合用FileProvider仍沒有解決的同胞們,亦或者直接用此方法:比較流氓的方法,繞過7.0的文件權(quán)限檢查

這里以打開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之前打開文件方式

public class MyIntent {?

? ? //android獲取一個(gè)用于打開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è)用于打開圖片文件的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è)用于打開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è)用于打開文本文件的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è)用于打開音頻文件的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è)用于打開視頻文件的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è)用于打開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è)用于打開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è)用于打開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è)用于打開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;?

? ? }?

}

作者:夢乾

來源:簡書

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 6,551評論 0 17
  • 只簡述我發(fā)現(xiàn)問題的根源,有些是適配了7.0,會(huì)報(bào)權(quán)限失敗問題,那是由于沒有動(dòng)態(tài)授權(quán)導(dǎo)致,接下來我一步一步給大家實(shí)現(xiàn)...
    Wocus閱讀 2,402評論 4 5
  • Intent組件雖然不是四大組件,但卻是連接四大組件的橋梁,學(xué)習(xí)好這個(gè)知識,也非常的重要。 一、什么是Intent...
    困惑困惑困惑閱讀 1,551評論 0 0
  • 常常會(huì)對自己的生活現(xiàn)狀不滿,對生活的周遭產(chǎn)生抱怨。對未來又期待又害怕。然后開始對未來做很多規(guī)劃。產(chǎn)生對自己...
    栯青閱讀 420評論 0 0
  • 涼爽的秋風(fēng)帶給人的是靜謐與祥和,而我此時(shí)的心情卻平靜不下來,被家長如驕陽似火般的熱情著實(shí)感動(dòng)了一番。 ...
    將深眠的紅薯叫醒閱讀 383評論 1 3