打开方式
AdapterView.OnItemLongClickListener itemchivklong = new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { FileUtils.openFiles(mContext, mFilePath.get(position)); return false; } };
FileUtils:
public static void openFiles(Context context,String filesPath) { Uri uri = Uri.parse("file://" + filesPath); Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); String type = getMIMEType(filesPath); intent.setDataAndType(uri, type); if (!type.equals("*/*")) { try { context. startActivity(intent); } catch (Exception e) { context. startActivity(showOpenTypeDialog(filesPath)); } } else { context. startActivity(showOpenTypeDialog(filesPath)); } }
public class FileUtils{
public void show(Context context, String filesPath) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); context.startActivity(showOpenTypeDialog(filesPath)); } public static Intent showOpenTypeDialog(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; } /** * --获取文件mimetype -- */ public static String getMIMEType(String filePath) { String type = "*/*"; String fName = filePath; int dotIndex = fName.lastIndexOf("."); if (dotIndex < 0) { return type; } String end = fName.substring(dotIndex, fName.length()).toLowerCase(); if (end == "") { return type; } for (int i = 0; i < MIME_MapTable.length; i++) { if (end.equals(MIME_MapTable[i][0])) { type = MIME_MapTable[i][1]; } } return type; } /** * -- MIME 列表 -- */ public static final String[][] MIME_MapTable = { // --{后缀名, MIME类型} -- { ".3gp", "video/3gpp"}, { ".3gpp", "video/3gpp"}, { ".aac", "audio/x-mpeg"}, { ".amr", "audio/x-mpeg"}, { ".apk", "application/vnd.android.package-archive"}, { ".avi", "video/x-msvideo"}, { ".aab", "application/x-authoware-bin"}, { ".aam", "application/x-authoware-map"}, { ".aas", "application/x-authoware-seg"}, { ".ai", "application/postscript"}, { ".aif", "audio/x-aiff"}, { ".aifc", "audio/x-aiff"}, { ".aiff", "audio/x-aiff"}, { ".als", "audio/x-alpha5"}, { ".amc", "application/x-mpeg"}, { ".ani", "application/octet-stream"}, { ".asc", "text/plain"}, { ".asd", "application/astound"}, { ".asf", "video/x-ms-asf"}, { ".asn", "application/astound"}, { ".asp", "application/x-asap"}, { ".asx", " video/x-ms-asf"}, { ".au", "audio/basic"}, { ".avb", "application/octet-stream"}, { ".awb", "audio/amr-wb"}, { ".bcpio", "application/x-bcpio"}, { ".bld", "application/bld"}, { ".bld2", "application/bld2"}, { ".bpk", "application/octet-stream"}, { ".bz2", "application/x-bzip2"}, { ".bin", "application/octet-stream"}, { ".bmp", "image/bmp"}, { ".c", "text/plain"}, { ".class", "application/octet-stream"}, { ".conf", "text/plain"}, { ".cpp", "text/plain"}, { ".cal", "image/x-cals"}, { ".ccn", "application/x-cnc"}, { ".cco", "application/x-cocoa"}, { ".cdf", "application/x-netcdf"}, { ".cgi", "magnus-internal/cgi"}, { ".chat", "application/x-chat"}, { ".clp", "application/x-msclip"}, { ".cmx", "application/x-cmx"}, { ".co", "application/x-cult3d-object"}, { ".cod", "image/cis-cod"}, { ".cpio", "application/x-cpio"}, { ".cpt", "application/mac-compactpro"}, { ".crd", "application/x-mscardfile"}, { ".csh", "application/x-csh"}, { ".csm", "chemical/x-csml"}, { ".csml", "chemical/x-csml"}, { ".css", "text/css"}, { ".cur", "application/octet-stream"}, { ".doc", "application/msword"}, { ".dcm", "x-lml/x-evm"}, { ".dcr", "application/x-director"}, { ".dcx", "image/x-dcx"}, { ".dhtml", "text/html"}, { ".dir", "application/x-director"}, { ".dll", "application/octet-stream"}, { ".dmg", "application/octet-stream"},