Android 檔案開啟方式
阿新 • • 發佈:2019-01-21
- publicstatic Intent openFile(String filePath){
- File file = new File(filePath);
- if(!file.exists()) returnnull;
- String end=file.getName().substring(file.getName().lastIndexOf(".") + 1,file.getName().length()).toLowerCase();
-
if(end.equals("m4a")||end.equals(
- end.equals("xmf")||end.equals("ogg")||end.equals("wav")){
- return getAudioFileIntent(filePath);
- }elseif(end.equals("3gp")||end.equals("mp4")){
- return getAudioFileIntent(filePath);
-
}elseif(end.equals(
- end.equals("jpeg")||end.equals("bmp")){
- return getImageFileIntent(filePath);
- }elseif(end.equals("apk")){
- return getApkFileIntent(filePath);
- }elseif(end.equals("ppt")){
-
return
- }elseif(end.equals("xls")){
- return getExcelFileIntent(filePath);
- }elseif(end.equals("doc")){
- return getWordFileIntent(filePath);
- }elseif(end.equals("pdf")){
- return getPdfFileIntent(filePath);
- }elseif(end.equals("chm")){
- return getChmFileIntent(filePath);
- }elseif(end.equals("txt")){
- return getTextFileIntent(filePath,false);
- }else{
- return getAllIntent(filePath);
- }
- }
- //Android獲取一個用於開啟APK檔案的intent
- publicstatic Intent getAllIntent( String param ) {
- Intent intent = new Intent();
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setAction(android.content.Intent.ACTION_VIEW);
- Uri uri = Uri.fromFile(new File(param ));
- intent.setDataAndType(uri,"*/*");
- return intent;
- }
- //Android獲取一個用於開啟APK檔案的intent
- publicstatic Intent getApkFileIntent( String param ) {
- Intent intent = new Intent();
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setAction(android.content.Intent.ACTION_VIEW);
- Uri uri = Uri.fromFile(new File(param ));
- intent.setDataAndType(uri,"application/vnd.android.package-archive");
- return intent;
- }
- //Android獲取一個用於開啟VIDEO檔案的intent
- publicstatic 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獲取一個用於開啟AUDIO檔案的intent
- publicstatic 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獲取一個用於開啟Html檔案的intent
- publicstatic 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獲取一個用於開啟圖片檔案的intent
- publicstatic 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獲取一個用於開啟PPT檔案的intent
- publicstatic 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;
- }
- //Android獲取一個用於開啟Excel檔案的intent
- publicstatic 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獲取一個用於開啟Word檔案的intent
- publicstatic 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獲取一個用於開啟CHM檔案的intent
- publicstatic 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獲取一個用於開啟文字檔案的intent
- publicstatic 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獲取一個用於開啟PDF檔案的intent
- publicstatic Intent getPdfFileIntent( String param ){
- Intent intent = new Intent("android.intent.action.VIEW");