//拍照和相册图片获取,一下是完整的一个例子 if (index == 0) {//拍照 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPathCover.getFile1())); startActivityForResult(intent, RESULT_CAPTURE); } else {// 相册 Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "请选择图片"), RESULT_PICK); } //选择后返回结果@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case RESULT_CAPTURE: if (resultCode == RESULT_OK) { LogUtils.e(mPathCover.getFile1().length() + ":-----------------"); CropPhoto(Uri.fromFile(mPathCover.getFile1())); } break; case RESULT_PICK: if (resultCode == RESULT_OK) { Uri uri = data.getData(); getImageExifInterfaceData(uri); CropPhoto(uri); } break; case CROP_PHOTO: if (resultCode == RESULT_OK) { // 刷新本地显示 if (data != null) { Uri uri = data.getData(); if (uri == null) { return; }getImageExifInterfaceData(uri); photo_im.setImageURI(uri); } } break; default: break; }}//裁剪的照片是没有附属信息的,需要我们自己写入,写入和读取方法请之前的博客中已经说明
// 裁剪照片public void CropPhoto(Uri uri) { if (uri == null) { return; } String path = getRealFilePathFromUri(this, uri); if (VZyImageUtil.checkImgDamage(path)) { showToast("图片可能损坏!"); return; } Intent intent = new Intent(); intent.setClass(this, ClipHeaderActivity.class); intent.setData(uri); intent.putExtra("side_length", 500);//裁剪图片宽高 startActivityForResult(intent, CROP_PHOTO); } // 根据uri 获取图片的绝对路径public static String getRealFilePathFromUri( final Context context, final Uri uri ) { if ( null == uri ) return null; final String scheme = uri.getScheme(); String data = null; if ( scheme == null ) data = uri.getPath(); else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) { data = uri.getPath(); } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) { Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null ); if ( null != cursor ) { if ( cursor.moveToFirst() ) { int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA ); if ( index > -1 ) { data = cursor.getString( index ); } } cursor.close(); } } return data; }
android 拍照和相册图片获取
最新推荐文章于 2024-09-14 09:52:48 发布