android 调用camera相关操作
16lz
2022-05-25
1,通过intent调用camera
然后onActivityResult()中
2,如果要调用gallery去剪辑的话
doCropPhoto(photo)
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");File photo = new File(Environment.getExternalStorageDirectory(),getPhotoFileName());Log.i(TAG,"getPhotoFileName():" +getPhotoFileName());intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));imageUri = Uri.fromFile(photo);startActivityForResult(intent, GET_PHOTO_WITH_CAMERA);private String getPhotoFileName() {Date date = new Date(System.currentTimeMillis());SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyy-MM-dd_HH-mm-ss");return dateFormat.format(date) + ".jpg";}
然后onActivityResult()中
if (requestCode == GET_PHOTO_WITH_CAMERA) {// doCropPhoto(mCurrentPhotoFile);Uri selectedImage = imageUri;Log.i(TAG, "uri:" + imageUri.toString());getContentResolver().notifyChange(selectedImage, null);ContentResolver cr = getContentResolver();Bitmap bitmap;try {bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);BitmapDrawable bd = new BitmapDrawable(bitmap);headerImage.setBackgroundDrawable(bd);} catch (Exception e) {Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();}}
2,如果要调用gallery去剪辑的话
doCropPhoto(photo)
protected void doCropPhoto(File f) {try {// 启动gallery去剪辑这个照片final Intent intent = getCropImageIntent(Uri.fromFile(f));startActivityForResult(intent, GET_PHOTO_WITH_GALLARY);} catch (Exception e) {}}public static Intent getCropImageIntent(Uri photoUri) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(photoUri, "image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);intent.putExtra("outputX", 80);intent.putExtra("outputY", 80);intent.putExtra("return-data", true);return intent;}
更多相关文章
- android实践项目八做一个下载读条
- Android调用RESTful WCF服务
- android过滤第三应用列表
- Android启动和关闭Activity
- Android的EditText无法自动弹出输入法问题
- 2011.07.12——— android Foreground service
- Android(安卓)根据包名杀死应用后台进程
- Android实现开机自启动及开机自启动失败原因
- 新建android opengl工程