android中调用系统功能 来显示本地相册图片 拍照 视频 音频功能

效果图如下:

android中调用系统功能 来显示本地相册图片 拍照 视频 音频功能_第1张图片

android中调用系统功能 来显示本地相册图片 拍照 视频 音频功能_第2张图片


本地相册跟拍照可直接调用系统功能

Intent img = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);FileNames name = new FileNames();File appFile = createRootDirectory(APP_FILE_NAMES);File file = new File(appFile.getPath(),name.getImageName());this.setImgFile(file);img.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));context.startActivityForResult(img, flag);
Intent getAlbum = new Intent(Intent.ACTION_OPEN_DOCUMENT);getAlbum.setType("image/*");context.startActivityForResult(getAlbum, flag);

视频调用 在不同手机会出现录制失败 需要在Android4.4以上版本Uri转换

/*** 根据Uri获取图片绝对路径,解决Android4.4以上版本Uri转换 * @param imageUri*/@TargetApi(19)public String getImageAbsolutePath(Uri imageUri) {    if (context == null || imageUri == null)            return null;    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, imageUri)) {    if (isExternalStorageDocument(imageUri)) {        String docId = DocumentsContract.getDocumentId(imageUri);                String[] split = docId.split(":");                String type = split[0];                if ("primary".equalsIgnoreCase(type)) {                    return Environment.getExternalStorageDirectory() + "/" + split[1];                }            } else if (isDownloadsDocument(imageUri)) {                String id = DocumentsContract.getDocumentId(imageUri);                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));                return getDataColumn(contentUri, null, null);            } else if (isMediaDocument(imageUri)) {                String docId = DocumentsContract.getDocumentId(imageUri);                String[] split = docId.split(":");                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;                }                String selection = MediaStore.Images.Media._ID + "=?";                String[] selectionArgs = new String[] { split[1] };                return getDataColumn(contentUri, selection, selectionArgs);            }        } // MediaStore (and general)        else if ("content".equalsIgnoreCase(imageUri.getScheme())) {            // Return the remote address            if (isGooglePhotosUri(imageUri))                return imageUri.getLastPathSegment();            return getDataColumn(imageUri, null, null);        }        // File        else if ("file".equalsIgnoreCase(imageUri.getScheme())) {            return imageUri.getPath();        }        return null;    }    private String getDataColumn(Uri uri, String selection, String[] selectionArgs) {        Cursor cursor = null;        String column = MediaStore.Images.Media.DATA;        String[] projection = { column };        try {            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);            if (cursor != null && cursor.moveToFirst()) {                int index = cursor.getColumnIndexOrThrow(column);                return cursor.getString(index);            }        } finally {            if (cursor != null)                cursor.close();        }        return null;    }
要根据uri来重新获取绝对路径

音频录制跟视频类似 不同型号手机不一定都能获取到,都是需通过uri来转换路径显示

在点击视频播放调用系统功能 部分会闪退 主要是手机型号,

Intent video = new Intent(Intent.ACTION_VIEW);video.setDataAndType(Uri.parse(path), "video/*");context.startActivity(video);

决解方案 就是直接使用自带的VideoView来播放视频

Intent intent = new Intent(mContext, VideoPlayActivity.class);Bundle bundle = new Bundle();bundle.putString("path",mList.get(pos).getPath());intent.putExtras(bundle);
videoView1 = (VideoView) findViewById(R.id.videoView1);Bundle bundle = getIntent().getExtras();file = bundle.getString("path");//获得拍摄的视频保存地址videoView1.setVideoPath(file);videoView1.start();videoView1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {      @Override      public void onCompletion(MediaPlayer mp) {           finish();      }});

DEMO下载

更多相关文章

  1. Android 打开相机、相册获取图片文件,支持Android 9.0系统
  2. 关于Android LOG系统
  3. 实现调用Android手机的拍照功能
  4. [置顶] android调用系统相机拍照并保存在本地
  5. Android调用系统下载服务下载文件
  6. android 获取手机系统信息
  7. 编译Android时,添加或者删除system.img中第三方apk、更改Android
  8. [Android] 监听系统网络连接打开或者关闭的消息
  9. js判断手机系统类型

随机推荐

  1. android google map apiKey 相关操作
  2. Android ListView中的Button屏蔽onItemCl
  3. Windows上搭建android开发环境
  4. Android IOS WebRTC 音视频开发总结(十五)-
  5. Android异步加载图像小结
  6. Android Ant更新项目
  7. 系出名门Android(8) - 控件(View)之TextS
  8. android中 google map计算两GPS点间距离
  9. Android实现TextView字符串波浪式跳动
  10. 页面未随软键盘上升及android隐藏软键盘