这两个应用操作本质上就是通过activity的action属性来调用相应的activity。
调用相机核心代码:
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //指向拍照activity的intent
activity.startActivityForResult(camera, GMJValues.CAMERA); //camera为自己设置的request code
调用相册的核心代码:
Intent picture = new Intent(Intent.ACTION_GET_CONTENT);
picture.setType("image/*");
activity.startActivityForResult(picture, GMJValues.PICTURE);
第一句为启动指向显示文件的activity,第二句为设置显示的文件的路径。
其他类似的调用:
//视频
Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
innerIntent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";
Intent wrapperIntent = Intent.createChooser(innerIntent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//添加音频
Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
innerIntent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";
Intent wrapperIntent = Intent.createChooser(innerIntent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//录音
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContentType.AUDIO_AMR); //String AUDIO_AMR = "audio/amr";
intent.setClassName("com.android.soundrecorder",
"com.android.soundrecorder.SoundRecorder");
((Activity) context).startActivityForResult(intent, requestCode);
//拍摄视频
int durationLimit = getVideoCaptureDurationLimit(); //SystemProperties.getInt("ro.media.enc.lprof.duration", 60);
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, sizeLimit);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit);
startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
onActivityResult中的返回值:
文件类的返回的是uri,
获得方式是:
Uri uri = data.getData();
Cursor cursor = activity.getContentResolver().query(uri, null,
null, null, null);
cursor.moveToFirst();
String imgPath = cursor.getString(1); // 图片文件路径
cursor.close();
其他的方式是直接获得资源文件,比如图片:
Bundle bundle = data.getExtras(); //data为onActivityResult方法的参数intent
bitmap = (Bitmap) bundle.get("data");
来源:https://www.cnblogs.com/lizebofree/p/3600576.html