Android选择图片或视频进行循环播放
程序员文章站
2023-12-13 22:47:58
项目要求对本地图片或者视频进行轮播,功能实现完成后发现只是在模拟器上运行ok,后来发现是文件路径的原因。
文件uri的头部有两种一种是以file开头一种是以content...
项目要求对本地图片或者视频进行轮播,功能实现完成后发现只是在模拟器上运行ok,后来发现是文件路径的原因。
文件uri的头部有两种一种是以file开头一种是以content开头要进行判断转化
实现如下:
视频 点击吊起文件查看:
private void setvideopath() { intent intent = new intent(intent.action_get_content); intent.settype("*/*");//设置类型,我这里是任意类型,任意后缀的可以这样写。 intent.addcategory(intent.category_openable); startactivityforresult(intent, video_path); }
在返回中取得选中文件路径
@override public void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode != result_ok) return; switch (requestcode) { case video_path: uri uri = data.getdata(); string path = getpath( uri); showtoastreal("你选中的视频路径:" + path); sputils.getinstace(this).savestring("videopath", path); break; case pic_path: uri picuri = data.getdata(); string picpath = getpath(picuri); showtoastreal("你选中的图片路径:" + picpath); sputils.getinstace(this).savestring("picpath", picpath); break; } }
public string getpath(uri uri) { string path; if ("file".equalsignorecase(uri.getscheme())) {//使用第三方应用打开 path = uri.getpath(); return path; } if (build.version.sdk_int > build.version_codes.kitkat) {//4.4以后 path = getpath(this, uri); } else {//4.4以下下系统调用方法 path = getrealpathfromuri(uri); } return path; } @suppresslint("newapi") public string getpath(final context context, final uri uri) { final boolean iskitkat = build.version.sdk_int >= build.version_codes.kitkat; // documentprovider if (iskitkat && documentscontract.isdocumenturi(context, uri)) { // externalstorageprovider if (isexternalstoragedocument(uri)) { final string docid = documentscontract.getdocumentid(uri); final string[] split = docid.split(":"); final string type = split[0]; if ("primary".equalsignorecase(type)) { return environment.getexternalstoragedirectory() + "/" + split[1]; } } // downloadsprovider else if (isdownloadsdocument(uri)) { final string id = documentscontract.getdocumentid(uri); final uri contenturi = contenturis.withappendedid( uri.parse("content://downloads/public_downloads"), long.valueof(id)); return getdatacolumn(context, contenturi, null, null); } // mediaprovider else if (ismediadocument(uri)) { final string docid = documentscontract.getdocumentid(uri); final string[] split = docid.split(":"); final string type = split[0]; uri contenturi = null; if ("image".equals(type)) { contenturi = mediastore.images.media.external_content_uri; } else if ("video".equals(type)) { contenturi = mediastore.video.media.external_content_uri; } else if ("audio".equals(type)) { contenturi = mediastore.audio.media.external_content_uri; } final string selection = "_id=?"; final string[] selectionargs = new string[]{split[1]}; return getdatacolumn(context, contenturi, selection, selectionargs); } } // mediastore (and general) else if ("content".equalsignorecase(uri.getscheme())) { return getdatacolumn(context, uri, null, null); } // file else if ("file".equalsignorecase(uri.getscheme())) { return uri.getpath(); } return null; } /** * get the value of the data column for this uri. this is useful for * mediastore uris, and other file-based contentproviders. * * @param context the context. * @param uri the uri to query. * @param selection (optional) filter used in the query. * @param selectionargs (optional) selection arguments used in the query. * @return the value of the _data column, which is typically a file path. */ public string getdatacolumn(context context, uri uri, string selection, string[] selectionargs) { cursor cursor = null; final string column = "_data"; final string[] projection = {column}; try { cursor = context.getcontentresolver().query(uri, projection, selection, selectionargs, null); if (cursor != null && cursor.movetofirst()) { final int column_index = cursor.getcolumnindexorthrow(column); return cursor.getstring(column_index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri the uri to check. * @return whether the uri authority is externalstorageprovider. */ public boolean isexternalstoragedocument(uri uri) { return "com.android.externalstorage.documents".equals(uri.getauthority()); } /** * @param uri the uri to check. * @return whether the uri authority is downloadsprovider. */ public boolean isdownloadsdocument(uri uri) { return "com.android.providers.downloads.documents".equals(uri.getauthority()); } /** * @param uri the uri to check. * @return whether the uri authority is mediaprovider. */ public boolean ismediadocument(uri uri) { return "com.android.providers.media.documents".equals(uri.getauthority()); }
实现视频轮播
public class videoactivity extends baseactivity { @bind(r.id.sv_ad) surfaceview vv; @bind(r.id.id_ig_back) imageview idigback; private mediaplayer mplayer; private string path; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_video); butterknife.bind(this); verifystoragepermissions(this); init(); // init2(); } private void init2() { string path = sputils.getinstace(videoactivity.this).getstring("videopath"); uri uri = uri.parse("file://" + path); try { mediaplayer mediaplayer = new mediaplayer(); mediaplayer.setaudiostreamtype(audiomanager.stream_music); mediaplayer.setdatasource(getapplicationcontext(), uri); mediaplayer.prepare(); mediaplayer.start(); } catch (ioexception e) { e.printstacktrace(); } } private void init() { idigback.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { finish(); } }); vv.getholder().addcallback(new surfaceholder.callback() { @override public void surfacedestroyed(surfaceholder holder) { if (mplayer != null) { mplayer.stop(); mplayer.release(); mplayer = null; } } @override public void surfacecreated(surfaceholder holder) { path = sputils.getinstace(videoactivity.this).getstring("videopath"); try { if (mplayer == null) { mplayer = mediaplayer.create(videoactivity.this, uri.parse("file://" + path)); } if(mplayer==null){ showtoastreal("请在个人中心中选择正确的视频"); } mplayer.setdisplay(holder);//将surfaceholder关联mediaplayer mplayer.setlooping(true); mplayer.start(); mplayer.setonerrorlistener(new mediaplayer.onerrorlistener() { @override public boolean onerror(mediaplayer mp, int what, int extra) { // todo auto-generated method stub return false; } }); } catch (exception e) { e.printstacktrace(); } } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } }); } public void onback(view view) { finish(); } @override public void loadnetdata() { } private static final int request_external_storage = 1; private static string[] permissions_storage = { manifest.permission.read_external_storage, manifest.permission.write_external_storage }; /** * 检查应用程序是否允许写入存储设备 * <p> * <p> * <p> * 如果应用程序不允许那么会提示用户授予权限 * * @param activity */ public static void verifystoragepermissions(activity activity) { // check if we have write permission int permission = activitycompat.checkselfpermission(activity, manifest.permission.write_external_storage); if (permission != packagemanager.permission_granted) { // we don't have permission so prompt the user activitycompat.requestpermissions( activity, permissions_storage, request_external_storage ); } } }
layout的实现
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <surfaceview android:id="@+id/sv_ad" android:layout_width="match_parent" android:layout_height="match_parent" /> <imageview android:id="@+id/id_ig_back" android:layout_width="80dp" android:layout_height="80dp" android:layout_margintop="16dp" android:padding="16dip" android:src="@drawable/icon_back_white" /> </relativelayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。