云豹app直播源码Android 端获取相册图片功能的具体实现教程
app直播源码开发时,关于动态功能的实现,通常采用类似于朋友圈式图文+视频模式,这就需要通过访问手机相册获取用户图片进行下一步操作,接下来就请跟随小编一起,从app直播源码角度探究云豹直播系统在这个功能上的具体实现方式。
1.如图1所示,当用户选择发布图片动态时,app页面上会弹出选择框,并将其他部分设定为深色不允许选中,用户可以选择拍照或本地相册获取图片两种方法选择图片
2.当选择本地图片时,将进入图2中的系统相册中进行图片选择,如果选择拍照则会进入相机拍照页面。
3.此外,云豹直播还自定义了图片选择器,如图3所示,将拍照和相册选择集中在一个页面,且支持多选。
部分app直播源码如下:
public class MediaUtil { public static void getImageByCamera(final FragmentActivity activity, final boolean needCrop, final ImageResultCallback imageResultCallback) { //请求拍照和存储的权限的回调 PermissionCallback permissionCallback = new PermissionCallback() { @Override public void onAllGranted() { if (imageResultCallback != null) { imageResultCallback.beforeCamera(); } Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); final File cameraResult = getNewFile(); Uri uri = null; if (Build.VERSION.SDK_INT >= 24) { uri = FileProvider.getUriForFile(activity, FILE_PROVIDER, cameraResult); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } else { uri = Uri.fromFile(cameraResult); } final Uri finalURI = uri; intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); //开始拍照 ActivityResultUtil.startActivityForResult(activity, intent, new ActivityResultCallback() { @Override public void onSuccess(Intent intent) { if (needCrop) {//需要裁剪 if (finalURI != null) { crop(activity, finalURI, imageResultCallback); } } else { if (imageResultCallback != null) { imageResultCallback.onSuccess(cameraResult); } } } @Override public void onFailure() { ToastUtil.show(R.string.img_camera_cancel); } }); } }; //app直播源码请求拍照和存储的权限 PermissionUtil.request( activity, permissionCallback, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA ); } /** * 拍照获取图片 */ public static void getImageByCamera(FragmentActivity activity, ImageResultCallback imageResultCallback) { getImageByCamera(activity, true, imageResultCallback); } /** * 相册获取图片 */ public static void getImageByAlumb(final FragmentActivity activity, final boolean needCrop, final ImageResultCallback imageResultCallback) { //请求存储的权限的回调 PermissionCallback permissionCallback = new PermissionCallback() { @Override public void onAllGranted() { Intent intent = new Intent(); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); if (Build.VERSION.SDK_INT < 19) { intent.setAction(Intent.ACTION_GET_CONTENT); } else { intent.setAction(Intent.ACTION_OPEN_DOCUMENT); } ActivityResultUtil.startActivityForResult(activity, intent, new ActivityResultCallback() { @Override public void onSuccess(Intent intent) { Uri dataUri = intent.getData(); if (dataUri != null) { if (needCrop) { crop(activity, dataUri, imageResultCallback); } else { if (imageResultCallback != null) { String path = FileUtils.getPath(activity, dataUri); if (!TextUtils.isEmpty(path)) { imageResultCallback.onSuccess(new File(path)); } } } } } @Override public void onFailure() { ToastUtil.show(R.string.img_alumb_cancel); } }); } }; //请求存储的权限 PermissionUtil.request( activity, permissionCallback, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE ); } public static void getImageByAlumb(FragmentActivity activity, ImageResultCallback imageResultCallback) { getImageByAlumb(activity, true, imageResultCallback); } public static void startVideoRecord(final FragmentActivity activity, final VideoResultCallback videoResultCallback) { PermissionCallback permissionCallback = new PermissionCallback() { @Override public void onAllGranted() { Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);// 表示跳转至相机的录视频界面 final File videoResult = getNewVideoFile(); Uri uri = null; if (Build.VERSION.SDK_INT >= 24) { uri = FileProvider.getUriForFile(activity, FILE_PROVIDER, videoResult); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } else { uri = Uri.fromFile(videoResult); } intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 15); //开始录制 ActivityResultUtil.startActivityForResult(activity, intent, new ActivityResultCallback() { @Override public void onSuccess(Intent intent) { if (intent != null && intent.getData() != null && videoResultCallback != null) { if (videoResult != null && videoResult.exists() && videoResult.length() > 0) { String path = videoResult.getAbsolutePath(); long duration = 0; MediaMetadataRetriever mmr = null; try { mmr = new MediaMetadataRetriever(); mmr.setDataSource(path); String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); if (!TextUtils.isEmpty(durationStr) && StringUtil.isInt(durationStr)) { duration = Long.parseLong(durationStr); } } catch (Exception e) { e.printStackTrace(); } finally { if (mmr != null) { mmr.release(); } mmr = null; } saveVideoInfo(activity, path, duration); videoResultCallback.onSuccess(videoResult, duration); } } } @Override public void onFailure() { ToastUtil.show(R.string.record_cancel); } }); } }; PermissionUtil.request( activity, permissionCallback, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO ); } private static File getNewFile() { // 裁剪头像的绝对路径 File dir = new File(CommonAppConfig.CAMERA_IMAGE_PATH); if (!dir.exists()) { dir.mkdirs(); } return new File(dir, DateFormatUtil.getCurTimeString() + ".png"); } private static void crop(FragmentActivity activity, Uri inputUri, final ImageResultCallback imageResultCallback) { final File corpResult = getNewFile(); try { Uri resultUri = Uri.fromFile(corpResult); if (resultUri == null) { return; } UCrop uCrop = UCrop.of(inputUri, resultUri) .withAspectRatio(1, 1) .withMaxResultSize(400, 400); Intent intent = uCrop.getIntent(activity); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); ActivityResultUtil.startActivityForResult(activity, intent, new ActivityResultCallback() { @Override public void onSuccess(Intent intent) { if (imageResultCallback != null) { imageResultCallback.onSuccess(corpResult); } } @Override public void onFailure() { ToastUtil.show(R.string.img_crop_cancel); } }); } catch (Exception e) { try { Uri resultUri = FileProvider.getUriForFile(activity, FILE_PROVIDER, corpResult); if (resultUri == null) { return; } UCrop uCrop = UCrop.of(inputUri, resultUri) .withAspectRatio(1, 1) .withMaxResultSize(400, 400); Intent intent = uCrop.getIntent(activity); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); ActivityResultUtil.startActivityForResult(activity, intent, new ActivityResultCallback() { @Override public void onSuccess(Intent intent) { if (imageResultCallback != null) { imageResultCallback.onSuccess(corpResult); } } @Override public void onFailure() { ToastUtil.show(R.string.img_crop_cancel); } }); } catch (Exception e1) { e1.printStackTrace(); } } } private static File getNewVideoFile() { File dir = new File(CommonAppConfig.VIDEO_PATH_RECORD); if (!dir.exists()) { dir.mkdirs(); } return new File(dir, DateFormatUtil.getCurTimeString() + ".mp4"); } /** * 把视频保存到ContentProvider,在选择上传的时候能找到 */ public static void saveVideoInfo(Context context, String videoPath, long duration) { try { File videoFile = new File(videoPath); String fileName = videoFile.getName(); long currentTimeMillis = System.currentTimeMillis(); ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.TITLE, fileName); values.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName); values.put(MediaStore.MediaColumns.DATE_MODIFIED, currentTimeMillis); values.put(MediaStore.MediaColumns.DATE_ADDED, currentTimeMillis); values.put(MediaStore.MediaColumns.DATA, videoPath); values.put(MediaStore.MediaColumns.SIZE, videoFile.length()); values.put(MediaStore.Video.VideoColumns.DATE_TAKEN, currentTimeMillis); values.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4"); values.put(MediaStore.Video.VideoColumns.DURATION, duration); context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); } catch (Exception e) { e.printStackTrace(); } } }
做完以上这些,就实现了app直播源码获取用户手机中相册图片的功能,如您想了解更多关于app直播源码的开发知识,欢迎联系云豹科技,会有专门的负责人为您讲解。
声明:以上内容为云豹科技作者本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任www.yunbaokj.com