• 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     }
  • 相关阅读:
    Python【第五篇】模块、包、常用模块
    Python【第四篇】函数、内置函数、递归、装饰器、生成器和迭代器
    TCP三次握手、四次挥手
    分别用postman和python做post请求接口功能测试
    Python【第三篇】文件操作、字符编码
    Python【第二篇】运算符及优先级、数据类型及常用操作、深浅拷贝
    Python【第一篇】python安装、pip基本用法、变量、输入输出、流程控制、循环
    oracle在windows(含客户端工具pl/sql安装)下安装
    Python【初识篇】简介
    Web jsp开发自学——ajax+servlet+echarts+json+gson 实现ajax传输servlert和echarts的数据,可视化结果
  • 原文地址:https://www.cnblogs.com/dukc/p/5123201.html
Copyright © 2020-2023  润新知