Android 檔案開啟方式用setDataAndType方式
阿新 • • 發佈:2019-02-17
具體程式碼實現片段是:
- <pre name="code"class="java">Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setDataAndType(Uri.fromFile(new File(filePath)),"application/vnd.android.package-archive");
- startActivity(intent);
filePath是該檔案的絕對路徑。
1.開啟所有檔案型別
- Intent intent = new Intent();
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setAction(android.content.Intent.ACTION_VIEW);
- intent.setDataAndType(uri,"*/*");
- Intent intent = new Intent();
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setAction(android.content.Intent.ACTION_VIEW);
- intent.setDataAndType(uri,"application/vnd.android.package-archive");
- Intent intent = new Intent("android.intent.action.VIEW");
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-
intent.setDataAndType(uri, "video/*"
- Intent intent = new Intent("android.intent.action.VIEW");
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intent.setDataAndType(uri, "audio/*");
5.開啟HTML檔案
- Uri uri2 = Uri.parse(uri).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build();
- Intent intent = new Intent("android.intent.action.VIEW");
- intent.setDataAndType(uri2, "text/html");
- Intent intent = new Intent("android.intent.action.VIEW");
- intent.addCategory("android.intent.category.DEFAULT");
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setDataAndType(uri, "image/*");
- Intent intent = new Intent("android.intent.action.VIEW");
- intent.addCategory("android.intent.category.DEFAULT");
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
- Intent intent = new Intent("android.intent.action.VIEW");
- intent.addCategory("android.intent.category.DEFAULT");
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setDataAndType(uri, "application/vnd.ms-excel");
- Intent intent = new Intent("android.intent.action.VIEW");
- intent.addCategory("android.intent.category.DEFAULT");
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(uri, "application/msword");
- Intent intent = new Intent("android.intent.action.VIEW");
- intent.addCategory("android.intent.category.DEFAULT");
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(uri, "application/x-chm");
- Intent intent = new Intent("android.intent.action.VIEW");
- intent.addCategory("android.intent.category.DEFAULT");
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setDataAndType(uri1, "text/plain");
- Intent intent = new Intent("android.intent.action.VIEW");
- intent.addCategory("android.intent.category.DEFAULT");
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setDataAndType(uri, "application/pdf");