Android 解析标准的点击第三方文件管理器中的视频的intent
解析标准的第三方视频intent
private List<String> mCurPlayList = new ArrayList<String>(); private int mCurPosition = 0; // 当前的位置信息
if (intent != null && TextUtils.equals(intent.getAction(), Intent.ACTION_VIEW) && !TextUtils.isEmpty(intent.getType()) && intent.getType().contains("video")) { // 用来解析标准的第三方视频intent String data = intent.getDataString(); // 这个地方获取的是乱码需要解析 file:///mnt/udisk2/%E7%BE%A4%E6%98%9F%20-%20%E5%8C%97%E4%BA%AC%E7%A5%9D%E7%A6%8F%E4%BD%A0.mp4 String folder = data; if (!TextUtils.isEmpty(data)) { if (data.startsWith("file://")) { data = Uri.decode(data).substring(7); Logcat.d("initData replaceFirst data " + data); } if (data.lastIndexOf("/") > 0) { folder = data.substring(0, data.lastIndexOf("/")); Logcat.d("initData substring folder " + folder); } File file = new File(folder); VideoUtils.GetVideoFile(mCurPlayList, file); if (mCurPlayList != null && mCurPlayList.size() > 0) { mCurPosition = mCurPlayList.indexOf(data); } } Logcat.d("initData " + data + "; mCurPosition " + mCurPosition); }
// 遍历指定目录的视频文件
public static void GetVideoFile(final List<String> mList, File file) { if (mList == null) { Logcat.w("mList is null "); return; } if (file == null) { Logcat.w("file is null "); return; } mList.clear(); file.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (FileUtils.isVideo(file)) { mList.add(file.getAbsolutePath()); Logcat.d("initData AbsolutePath " + file.getAbsolutePath()); return true; } return false; } }); }
/** whether the file is video */ public static boolean isVideo(File file) { String surfix = getFileSurfix(file); return (surfix.equals("avi") || surfix.equals("mp4") || surfix.equals("3gp") || surfix.equals("rmvb") || surfix.equals("mov") || surfix.equals("wmv") || surfix.equals("mpeg") || surfix.equals("mpg") || surfix.equals("rm") || surfix.equals("vob") || surfix.equals("mkv") || surfix.equals("flv") || surfix.equals("asp") || surfix.equals("ASF") || surfix.equals("m2ts") || surfix.equals("ts") || surfix.equals("divx")); }
/** Get file suffix from file path */ public static String getFileSurfix(File file) { String surfix = ""; if (file != null) { String filePath = file.getAbsolutePath(); surfix = getFileSurfix(filePath); } return surfix.toLowerCase(); }
/** Get file suffix from file path */ public static String getFileSurfix(String filePath) { String surfix = ""; if (filePath != null) { int surfixPos = filePath.lastIndexOf("."); int surfixPosDivider = filePath.lastIndexOf("/"); // invalid : abcdefg.hijk/lmn if (surfixPos > 0 && surfixPos > surfixPosDivider) surfix = filePath.substring(surfixPos + 1, filePath.length()); } return surfix.toLowerCase(); }
<intent-filter> <action android:name="android.intent.action.VIEW" /> <data android:mimeType="video/*" android:scheme="http" /> <data android:mimeType="video/*" android:scheme="file" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
打印的日志:
11-02 09:34:03.739 2469-2469/? D/RRIVI: VideoPlayerActivity.java(318):initData: initData replaceFirst data /mnt/udisk2/Lady.GaGa.-.Poker.Face.avi 11-02 09:34:03.740 2469-2469/? D/RRIVI: VideoPlayerActivity.java(322):initData: initData substring folder /mnt/udisk2 11-02 09:34:03.744 2469-2469/? D/RRIVI: VideoUtils.java(86):accept: initData AbsolutePath /mnt/udisk2/Wildlife.wmv 11-02 09:34:03.744 2469-2469/? D/RRIVI: VideoUtils.java(86):accept: initData AbsolutePath /mnt/udisk2/江南丝带舞.mp4 11-02 09:34:03.745 2469-2469/? D/RRIVI: VideoUtils.java(86):accept: initData AbsolutePath /mnt/udisk2/群星 - 北京祝福你.mp4 11-02 09:34:03.746 2469-2469/? D/RRIVI: VideoUtils.java(86):accept: initData AbsolutePath /mnt/udisk2/少女时代的-My Oh My.mp4 11-02 09:34:03.746 2469-2469/? D/RRIVI: VideoUtils.java(86):accept: initData AbsolutePath /mnt/udisk2/Lady.GaGa.-.Poker.Face.avi 11-02 09:34:03.747 2469-2469/? D/RRIVI: VideoUtils.java(86):accept: initData AbsolutePath /mnt/udisk2/mv_少女时代 - Gee.rmvb 11-02 09:34:03.747 2469-2469/? D/RRIVI: VideoPlayerActivity.java(330):initData: initData /mnt/udisk2/Lady.GaGa.-.Poker.Face.avi; mCurPosition 4