Android选择图片或视频进行循环播放

Android选择图片或视频进行循环播放,第1张

概述项目要求对本地图片或者视频进行轮播,功能实现完成后发现只是在模拟器上运行ok,后来发现是文件路径的原因。

项目要求对本地图片或者视频进行轮播,功能实现完成后发现只是在模拟器上运行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,selection,selectionArgs);       }     }     // MediaStore (and general)     else if ("content".equalsIgnoreCase(uri.getScheme())) {       return getDataColumn(context,uri,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,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> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android选择图片或视频进行循环播放全部内容,希望文章能够帮你解决Android选择图片或视频进行循环播放所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144012.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存