Android實(shí)現(xiàn)打開(kāi)各種文件的intent方法小結(jié)
本文實(shí)例講述了Android實(shí)現(xiàn)打開(kāi)各種文件的intent方法。分享給大家供大家參考,具體如下:
import android.app.Activity; import Android.content.Intent; import android.net.Uri; import android.net.Uri.Builder; import Java.io.File; import android.content.Intent; //自定義android Intent類, //可用于獲取打開(kāi)以下文件的intent //PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO
示例:
//這個(gè)不行,可能是因?yàn)镻DF.apk程序沒(méi)有權(quán)限訪問(wèn)其它APK里的asset資源文件,又或者是路徑寫(xiě)錯(cuò)?
//Intent it = getPdfFileIntent("file:///android_asset/helphelp.pdf");
//下面這些都OK
//Intent it = getHtmlFileIntent("/mnt/sdcard/tutorial.html");//SD卡主目錄
//Intent it = getHtmlFileIntent("/sdcard/tutorial.html");//SD卡主目錄,這樣也可以
Intent it = getHtmlFileIntent("/system/etc/tutorial.html");//系統(tǒng)內(nèi)部的etc目錄
//Intent it = getPdfFileIntent("/system/etc/helphelp.pdf");
//Intent it = getWordFileIntent("/system/etc/help.doc");
//Intent it = getExcelFileIntent("/mnt/sdcard/Book1.xls")
//Intent it = getPptFileIntent("/mnt/sdcard/download/Android_PPT.ppt");//SD卡的download目錄下
//Intent it = getVideoFileIntent("/mnt/sdcard/ice.avi");
//Intent it = getAudioFileIntent("/mnt/sdcard/ren.mp3");
//Intent it = getImageFileIntent("/mnt/sdcard/images/001041580.jpg");
//Intent it = getTextFileIntent("/mnt/sdcard/hello.txt",false);
startActivity( it );
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;
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android編程開(kāi)發(fā)之打開(kāi)文件的Intent及使用方法
- Android開(kāi)發(fā)實(shí)現(xiàn)的Intent跳轉(zhuǎn)工具類實(shí)例
- Android使用Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- Android使用Intent獲取聯(lián)系人信息
- Android Intent的幾種用法詳細(xì)解析
- Android Intent啟動(dòng)別的應(yīng)用實(shí)現(xiàn)方法
- 詳解Android中Intent的使用方法
- 詳解Android應(yīng)用開(kāi)發(fā)中Intent的作用及使用方法
- Android系列之Intent傳遞對(duì)象的幾種實(shí)例方法
- Android開(kāi)發(fā)中使用Intent打開(kāi)第三方應(yīng)用及驗(yàn)證可用性的方法詳解
相關(guān)文章
Android 滑動(dòng)定位和吸附懸停效果實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 滑動(dòng)定位和吸附懸停效果實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android 開(kāi)機(jī)應(yīng)用掃描相關(guān)總結(jié)
本篇文章只是作為指南引導(dǎo)去看PkMS,不會(huì)貼大段代碼進(jìn)行分析,更多是基于方法分析實(shí)現(xiàn)的邏輯,另外就是代碼是基于Android 11,與Android 10之前代碼有比較大的差別。2021-05-05
Android自定義谷歌風(fēng)格ProgressBar
這篇文章主要為大家詳細(xì)介紹了Android自定義谷歌風(fēng)格ProgressBar的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android Activity的4種啟動(dòng)模式圖文介紹
這篇文章主要給大家介紹了關(guān)于Android Activity的4種啟動(dòng)模式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Android應(yīng)用接入微信分享的實(shí)例代碼
本篇文章主要介紹了Android應(yīng)用接入微信分享的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-07-07
Android RecyclerView區(qū)分視圖類型的Divider的實(shí)現(xiàn)
本篇文章主要介紹了Android RecyclerView區(qū)分視圖類型的Divider的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Android編程簡(jiǎn)單實(shí)現(xiàn)撥號(hào)器功能的方法
這篇文章主要介紹了Android編程簡(jiǎn)單實(shí)現(xiàn)撥號(hào)器功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android撥號(hào)器功能的實(shí)現(xiàn)原理、步驟、操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07
Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值
這篇文章主要為大家詳細(xì)介紹了Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android TextWatcher內(nèi)容監(jiān)聽(tīng)死循環(huán)案例詳解
這篇文章主要介紹了Android TextWatcher內(nèi)容監(jiān)聽(tīng)死循環(huán)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

