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

来源:互联网 发布:php递归算法经典实例 编辑:程序博客网 时间:2024/05/18 17:05

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下载

1 0
原创粉丝点击