Created
October 8, 2014 02:47
-
-
Save bingoogolapple/6ae44cc8d6b908d16a28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IntentUtil { | |
private IntentUtil() { | |
} | |
public static Intent openFile(String filePath) { | |
if (ExtUtil.isAudio(filePath)) { | |
return getAudioFileIntent(filePath); | |
} else if (ExtUtil.isVideo(filePath)) { | |
return getVideoFileIntent(filePath); | |
} else if (ExtUtil.isImage(filePath)) { | |
return getImageFileIntent(filePath); | |
} else if (ExtUtil.isApk(filePath)) { | |
return getApkFileIntent(filePath); | |
} else if (ExtUtil.isPpt(filePath)) { | |
return getPptFileIntent(filePath); | |
} else if (ExtUtil.isExcel(filePath)) { | |
return getExcelFileIntent(filePath); | |
} else if (ExtUtil.isWord(filePath)) { | |
return getWordFileIntent(filePath); | |
} else if (ExtUtil.isPdf(filePath)) { | |
return getPdfFileIntent(filePath); | |
} else if (ExtUtil.isChm(filePath)) { | |
return getChmFileIntent(filePath); | |
} else if (ExtUtil.isTxt(filePath)) { | |
return getTextFileIntent(filePath, false); | |
} else { | |
return getAllIntent(filePath); | |
} | |
} | |
public static 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; | |
} | |
public static 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; | |
} | |
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; | |
} | |
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; | |
} | |
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; | |
} | |
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; | |
} | |
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; | |
} | |
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; | |
} | |
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; | |
} | |
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; | |
} | |
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; | |
} | |
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; | |
} | |
public static Intent getInstallApkIntent(File apkFile) { | |
Intent intent = new Intent(); | |
intent.setAction(Intent.ACTION_VIEW); | |
intent.addCategory(Intent.CATEGORY_DEFAULT); | |
intent.setDataAndType(Uri.fromFile(apkFile), Constants.mime.APK); | |
return intent; | |
} | |
public static Intent getTakePictureIntent(File imageFile) { | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
intent.addCategory(Intent.CATEGORY_DEFAULT); | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile)); | |
return intent; | |
} | |
public static Intent getSelectFileIntent(String type, String title) { | |
Intent intent = new Intent(); | |
intent.setType(type); | |
intent.setAction(Intent.ACTION_GET_CONTENT); | |
return Intent.createChooser(intent, title); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment