关于拍照相册读取裁剪图片显示图片的比较好的博客


转载自:http://blog.csdn.net/harvic880925/article/details/43163175


拍照及裁剪终极方案

首先声明两个Uri,一个保存拍照的结果,一个保存裁剪的结果:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. private static final int RESULT_CAMERA_ONLY = 100;  
  2. private static final int RESULT_CAMERA_CROP_PATH_RESULT = 301;  
  3. private ImageView mImage;  
  4. private Uri imageUri;  
  5. private Uri imageCropUri;  
然后在OnCreate()函数中初始化:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.activity_main);  
  4.   
  5.     String path = getSDCardPath();  
  6.     File file = new File(path + "/temp.jpg");  
  7.     imageUri = Uri.fromFile(file);  
  8.     File cropFile = new File(getSDCardPath() + "/temp_crop.jpg");  
  9.     imageCropUri = Uri.fromFile(cropFile);  
  10.   
  11.     mImage = (ImageView) findViewById(R.id.image_result);  
  12.     Button btn_take_camera_only = (Button) findViewById(R.id.btn_camera_only);  
  13.     btn_take_camera_only.setOnClickListener(new View.OnClickListener() {  
  14.         @Override  
  15.         public void onClick(View view) {  
  16.             takeCameraOnly();  
  17.         }  
  18.     });  
  19. }  
点击按钮时仅调起拍照Intent,将结果存在imageUri中

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. private void takeCameraOnly() {  
  2.     Intent intent = null;  
  3.     intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture  
  4.     intent.putExtra("return-data"false);  
  5.     intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);  
  6.     intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());  
  7.     intent.putExtra("noFaceDetection"true);  
  8.     startActivityForResult(intent, RESULT_CAMERA_ONLY);  
  9. }  
在接收到返回的消息后,调起裁剪Intent:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  2.     super.onActivityResult(requestCode, resultCode, data);  
  3.     if (resultCode != Activity.RESULT_OK)  
  4.         return;  
  5.     switch (requestCode) {  
  6.         case RESULT_CAMERA_ONLY: {  
  7.             cropImg(imageUri);  
  8.         }  
  9.         break;  
  10.     }  
  11. }  
其中cropImg(Uri uri)是调起裁剪Intent,代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void cropImg(Uri uri) {  
  2.     Intent intent = new Intent("com.android.camera.action.CROP");  
  3.     intent.setDataAndType(uri, "image/*");  
  4.     intent.putExtra("crop""true");  
  5.     intent.putExtra("aspectX"1);  
  6.     intent.putExtra("aspectY"1);  
  7.     intent.putExtra("outputX"700);  
  8.     intent.putExtra("outputY"700);  
  9.     intent.putExtra("return-data"false);  
  10.     intent.putExtra(MediaStore.EXTRA_OUTPUT, imageCropUri);  
  11.     intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());  
  12.     intent.putExtra("noFaceDetection"true);  
  13.     startActivityForResult(intent, RESULT_CAMERA_CROP_PATH_RESULT);  
  14. }  
将传进去的uri做为源数据,即被裁剪的数据,将结果存储在imageCropUri中;

然后接收返回的结果:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  2.     super.onActivityResult(requestCode, resultCode, data);  
  3.     if (resultCode != Activity.RESULT_OK)  
  4.         return;  
  5.     switch (requestCode) {  
  6.         case RESULT_CAMERA_ONLY: {  
  7.             cropImg(imageUri);  
  8.         }  
  9.         break;  
  10.         case RESULT_CAMERA_CROP_PATH_RESULT: {  
  11.             Bundle extras = data.getExtras();  
  12.             if (extras != null) {  
  13.                 try {  
  14.                     Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageCropUri));  
  15.                     mImage.setImageBitmap(bitmap);  
  16.                 } catch (Exception e) {  
  17.                     e.printStackTrace();  
  18.                 }  
  19.             }  
  20.         }  
  21.         break;  
  22.     }  
  23. }  
将存储裁剪结果的imageCropUri,转换为Bitmap,然后在ImageView中显示;
完整的代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.     private static final int RESULT_CAMERA_ONLY = 100;  
  3.     private static final int RESULT_CAMERA_CROP_PATH_RESULT = 301;  
  4.     private ImageView mImage;  
  5.     private Uri imageUri;  
  6.     private Uri imageCropUri;  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.   
  13.         String path = getSDCardPath();  
  14.         File file = new File(path + "/temp.jpg");  
  15.         imageUri = Uri.fromFile(file);  
  16.         File cropFile = new File(getSDCardPath() + "/temp_crop.jpg");  
  17.         imageCropUri = Uri.fromFile(cropFile);  
  18.   
  19.         mImage = (ImageView) findViewById(R.id.image_result);  
  20.         Button btn_take_camera_only = (Button) findViewById(R.id.btn_camera_only);  
  21.         btn_take_camera_only.setOnClickListener(new View.OnClickListener() {  
  22.             @Override  
  23.             public void onClick(View view) {  
  24.                 takeCameraOnly();  
  25.             }  
  26.         });  
  27.     }  
  28.   
  29.   
  30.     private void takeCameraOnly() {  
  31.         Intent intent = null;  
  32.         intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture  
  33.         intent.putExtra("return-data"false);  
  34.         intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);  
  35.         intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());  
  36.         intent.putExtra("noFaceDetection"true);  
  37.         startActivityForResult(intent, RESULT_CAMERA_ONLY);  
  38.     }  
  39.   
  40.     public void cropImg(Uri uri) {  
  41.         Intent intent = new Intent("com.android.camera.action.CROP");  
  42.         intent.setDataAndType(uri, "image/*");  
  43.         intent.putExtra("crop""true");  
  44.         intent.putExtra("aspectX"1);  
  45.         intent.putExtra("aspectY"1);  
  46.         intent.putExtra("outputX"700);  
  47.         intent.putExtra("outputY"700);  
  48.         intent.putExtra("return-data"false);  
  49.         intent.putExtra(MediaStore.EXTRA_OUTPUT, imageCropUri);  
  50.         intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());  
  51.         intent.putExtra("noFaceDetection"true);  
  52.         startActivityForResult(intent, RESULT_CAMERA_CROP_PATH_RESULT);  
  53.     }  
  54.   
  55.     @Override  
  56.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  57.         super.onActivityResult(requestCode, resultCode, data);  
  58.         if (resultCode != Activity.RESULT_OK)  
  59.             return;  
  60.         switch (requestCode) {  
  61.             case RESULT_CAMERA_ONLY: {  
  62.                 cropImg(imageUri);  
  63.             }  
  64.             break;  
  65.             case RESULT_CAMERA_CROP_PATH_RESULT: {  
  66.                 Bundle extras = data.getExtras();  
  67.                 if (extras != null) {  
  68.                     try {  
  69.                         Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageCropUri));  
  70.                         mImage.setImageBitmap(bitmap);  
  71.                     } catch (Exception e) {  
  72.                         e.printStackTrace();  
  73.                     }  
  74.                 }  
  75.             }  
  76.             break;  
  77.         }  
  78.     }  
  79.   
  80.     public static String getSDCardPath() {  
  81.         String cmd = "cat /proc/mounts";  
  82.         Runtime run = Runtime.getRuntime();// 返回与当前 Java 应用程序相关的运行时对象  
  83.         try {  
  84.             Process p = run.exec(cmd);// 启动另一个进程来执行命令  
  85.             BufferedInputStream in = new BufferedInputStream(p.getInputStream());  
  86.             BufferedReader inBr = new BufferedReader(new InputStreamReader(in));  
  87.   
  88.             String lineStr;  
  89.             while ((lineStr = inBr.readLine()) != null) {  
  90.                 // 获得命令执行后在控制台的输出信息  
  91.                 if (lineStr.contains("sdcard")  
  92.                         && lineStr.contains(".android_secure")) {  
  93.                     String[] strArray = lineStr.split(" ");  
  94.                     if (strArray != null && strArray.length >= 5) {  
  95.                         String result = strArray[1].replace("/.android_secure",  
  96.                                 "");  
  97.                         return result;  
  98.                     }  
  99.                 }  
  100.                 // 检查命令是否执行失败。  
  101.                 if (p.waitFor() != 0 && p.exitValue() == 1) {  
  102.                     // p.exitValue()==0表示正常结束,1:非正常结束  
  103.                 }  
  104.             }  
  105.             inBr.close();  
  106.             in.close();  
  107.         } catch (Exception e) {  
  108.   
  109.             return Environment.getExternalStorageDirectory().getPath();  
  110.         }  
  111.   
  112.         return Environment.getExternalStorageDirectory().getPath();  
  113.     }  
  114. }  


到这里,这篇文章就讲完了,写的比较乱,原理应该是讲清楚了,有关从相册选择及裁剪的部分在下篇中讲解。

源码内容:

1、BlogCameraOnly:仅拍照功能

2、BlogCameraCropCrash:第三部分对应的源码,根本起不来裁剪Intent,造成Crash

3、BlogCameraCropFinally:拍照及裁剪的终极方案;

如果本文有帮到你,记得关注哦

源码下载地址:http://download.csdn.net/detail/harvic880925/8412983

请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/43163175  谢谢。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值