zoukankan      html  css  js  c++  java
  • Android调用相册截取图片遇到的问题

    1.在Android中可以使用如下的方式来调用相册,选择图片进行裁剪使用,昨天在开发的时候遇到一个问题

      private void cropBigImageUri(Uri uri, int outputX, int outputY, int requestCode) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 641);
        intent.putExtra("aspectY", 361);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        startActivityForResult(intent, requestCode);
      }

    2.问题描述如下:

      在调用相册选择图片以后,需要对图片按照比列进行裁剪,裁剪后会修改原图。

    3.原因分析:

      intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);输出时传入的uri是选择的原图的uri,裁剪后新图片的输出保存会覆盖原图uri

    4.解决办法:

      不使用原图的uri进行保存,建立一个新的uri进行保存裁剪后的图片

      String sdcardPathDir = android.os.Environment.getExternalStorageDirectory().getPath() + "xxx/photo/";

      // 有sd卡,是否有myImage文件夹
      File fileDir = new File(sdcardPathDir);
      if (!fileDir.exists()) {
        fileDir.mkdirs();
      }

      // 获取系统时间 然后将裁剪后的图片保存至指定的文件夹
      SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
      address = sDateFormat.format(new java.util.Date());
      if (!FileUtils.isFileExist("")) {
        try {
          FileUtils.createSDDir("");
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }

      }

      // 这里需要和FileUtils.SDPATH 一致,而且在之前要创建文件夹
      Uri imageUri = Uri.parse("file:///sdcardxxxx/photo/thumb" + address + ".jpg");

      然后使用intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri );

    但行好事,莫问前程;你若盛开,蝴蝶自来;你若坚强,命运自会给你打赏。
  • 相关阅读:
    微信证书 javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    【转】ubunt 安装 yum出现 ubuntu 解决“无法获得锁 /var/lib/dpkg/lock -open (11:资源暂时不可用)”的方法
    HTTP Status 500
    idea导入项目报错:文档中根元素前面的标记必须格式正确
    redis 存取问题
    maven项目怎么引入另一个maven项目
    如何解决failed to push some refs to git
    idea配置maven后提示 commond not found
    JMS规范简介
    java消息中间件的使用与简介
  • 原文地址:https://www.cnblogs.com/songjie-xuan/p/5713890.html
Copyright © 2011-2022 走看看