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


    前言,版本兼容问题主要是由于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     }
  • 相关阅读:
    一般删除网页数据和jquery下使用Ajax删除数据的区别
    JavaScript 局部刷新
    ASP.net 网站开发知识点总结
    deque
    DHCP协议
    IP分类以及特殊IP
    重载运算符函数及其注意事项
    linux gdb基本概念
    std::vector 源代码
    iterator 的设计原则和traits
  • 原文地址:https://www.cnblogs.com/dukc/p/5123201.html
Copyright © 2020-2023  润新知