码迷,mamicode.com
首页 > 移动开发 > 详细

android 选择图片 剪裁 拍照 兼容所有版本的代码

时间:2016-01-12 08:49:01      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

前言,版本兼容问题主要是由于4.4以前和4.4以后的Uri的格式不同所造成的错误

  资料 Android 4.4从图库选择图片,获取图片路径并裁剪

1.拍照 和选择图片 

  ①选择图片

1 intent = new Intent(Intent.ACTION_GET_CONTENT);
2             intent.setType("image/*");
3             startActivityForResult(intent, GALLERY_REQUEST_CODE);

  ②拍照
  

1 intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
2             startActivityForResult(intent, CAMERA_REQUEST_CODE);

2.获取系统传来的值

 1 标记符
 2     private static int CAMERA_REQUEST_CODE = 1;
 3     private static int GALLERY_REQUEST_CODE = 2;
 4     private static int CROP_REQUEST_CODE = 3;
 5 
 6 
 7 
 8     @Override
 9     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
10         if (requestCode == CAMERA_REQUEST_CODE) {
11             if (data == null) {
12                 return;
13             } else { //拍照
14                 Bundle extras = data.getExtras();
15                 if (extras != null) {
16                     Bitmap bm = extras.getParcelable("data");
17                     Uri uri = saveBitmap(bm);
18                     startImageZoom(uri);
19                 }
20             }
21         } else if (requestCode == GALLERY_REQUEST_CODE) {
22             if (data == null) {//相册
23                 return;
24             }
25             Uri uri;
26             uri = data.getData();
27             Uri fileUri = convertUri(uri);
28             startImageZoom(fileUri);
29         } else if (requestCode == CROP_REQUEST_CODE) {
30             if (data == null) {
31                 return;
32             }//剪裁后的图片
33             Bundle extras = data.getExtras();
34             if (extras == null) {
35                 return;
36             }
37             Bitmap bm = extras.getParcelable("data");
38             ShowImageView(bm);
39         }
40     }

3.图片选取后 根据Url 转成流 并保存

private Uri convertUri(Uri uri) {
        InputStream is = null;
        try {
            is = getContentResolver().openInputStream(uri);
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            is.close();
            return saveBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

4.保存图片 记得加权限

 1 private Uri saveBitmap(Bitmap bm) {
 2         File tmpDir = new File(Environment.getExternalStorageDirectory()
 3                 + "/xiaoxin");
 4         if (!tmpDir.exists()) {
 5             tmpDir.mkdir();
 6         }
 7         File img = new File(tmpDir.getAbsolutePath() + "love.png");
 8         try {
 9             FileOutputStream fos = new FileOutputStream(img);
10             bm.compress(Bitmap.CompressFormat.PNG, 85, fos);
11             fos.flush();
12             fos.close();
13             Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_SHORT).show();
14             return Uri.fromFile(img);
15         } catch (FileNotFoundException e) {
16             Toast.makeText(MainActivity.this, "失敗了", Toast.LENGTH_SHORT).show();
17             e.printStackTrace();
18             return null;
19         } catch (IOException e) {
20             e.printStackTrace();
21             Toast.makeText(MainActivity.this, "失敗了", Toast.LENGTH_SHORT).show();
22             return null;
23         }
24 
25     }

5.剪裁图片

 1 /**
 2      * 剪裁图片
 3      * 
 4      * @param uri
 5      */
 6     private void startImageZoom(Uri uri) {
 7         Intent intent = new Intent("com.android.camera.action.CROP");
 8         intent.setDataAndType(uri, "image/*");
 9         intent.putExtra("crop", "true");
10         intent.putExtra("aspectX", 1);
11         intent.putExtra("aspectY", 1);
12         intent.putExtra("outputX", 150);
13         intent.putExtra("outputY", 150);
14         intent.putExtra("return-data", true);
15         startActivityForResult(intent, CROP_REQUEST_CODE);
16     }

 

android 选择图片 剪裁 拍照 兼容所有版本的代码

标签:

原文地址:http://www.cnblogs.com/dukc/p/5123201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!