android中调用系统功能 来显示本地相册图片 拍照 视频 音频功能
16lz
2021-01-23
android中调用系统功能 来显示本地相册图片 拍照 视频 音频功能
效果图如下:
本地相册跟拍照可直接调用系统功能
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下载
更多相关文章
- Android 打开相机、相册获取图片文件,支持Android 9.0系统
- 关于Android LOG系统
- 实现调用Android手机的拍照功能
- [置顶] android调用系统相机拍照并保存在本地
- Android调用系统下载服务下载文件
- android 获取手机系统信息
- 编译Android时,添加或者删除system.img中第三方apk、更改Android
- [Android] 监听系统网络连接打开或者关闭的消息
- js判断手机系统类型