资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android如何实现拍照及图片裁剪

这篇文章主要介绍Android如何实现拍照及图片裁剪,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

十余年的广灵网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整广灵建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“广灵网站设计”,“广灵网站推广”以来,每个客户项目都认真落实执行。

最近做项目中涉及到了图片相关功能 ,在使用安卓6.0手机及7.1手机拍照时,遇到了因权限及文件管理导致程序崩溃等问题。
 刚好把功能修改完,把代码简单地贴一下,方便以后使用。

—-主界面 代码 ——

public class MainActivity extends AppCompatActivity {
  //拍照按钮
  private Button take_photo;
  //显示裁剪后的图片
  private ImageView photo_iv;
  private static final int PERMISSIONS_FOR_TAKE_PHOTO = 10;
  //图片文件路径
  private String picPath;
  //图片对应Uri
  private Uri photoUri;
  //拍照对应RequestCode
  public static final int SELECT_PIC_BY_TACK_PHOTO = 1;
  //裁剪图片
  private static final int CROP_PICTURE = 3;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    take_photo = (Button) findViewById(R.id.take_photo);
    photo_iv = (ImageView) findViewById(R.id.photo_iv);
    take_photo.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        //小于6.0版本直接操作
        if (Build.VERSION.SDK_INT < 23) {
          takePictures();
        } else {
          //6.0以后权限处理
          permissionForM();
        }
      }
    });
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
      if (requestCode == SELECT_PIC_BY_TACK_PHOTO) {
        String[] pojo = {MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
        if (cursor != null) {
          int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
          cursor.moveToFirst();
          picPath = cursor.getString(columnIndex);
          if (Build.VERSION.SDK_INT < 14) {
            cursor.close();
          }
        }
        if (picPath != null && (picPath.endsWith(".png") || picPath.endsWith(".PNG") || picPath.endsWith(".jpg") || picPath.endsWith(".JPG"))) {
          photoUri = Uri.fromFile(new File(picPath));
          if (Build.VERSION.SDK_INT > 23) {
            photoUri = FileProvider.getUriForFile(this, "com.innopro.bamboo.fileprovider", new File(picPath));
            cropForN(picPath, CROP_PICTURE);
          } else {
            startPhotoZoom(photoUri, CROP_PICTURE);
          }
        } else {
          //错误提示
        }
      }
      if (requestCode == CROP_PICTURE) {
        if (photoUri != null) {
          Bitmap bitmap = BitmapFactory.decodeFile(picPath);
          if (bitmap != null) {
            photo_iv.setImageBitmap(bitmap);
          }
        }
      }
    }
  }

  /**
   * 拍照获取图片
   */
  private void takePictures() {
    //执行拍照前,应该先判断SD卡是否存在
    String SDState = Environment.getExternalStorageState();
    if (SDState.equals(Environment.MEDIA_MOUNTED)) {
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      ContentValues values = new ContentValues();
      photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
      intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
      startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
    } else {
      Toast.makeText(this, "手机未插入内存卡", Toast.LENGTH_LONG).show();
    }
  }


  /**
   * 图片裁剪,参数根据自己需要设置
   *
   * @param uri
   * @param REQUE_CODE_CROP
   */
  private void startPhotoZoom(Uri uri,
                int REQUE_CODE_CROP) {
    int dp = 500;
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
    intent.putExtra("crop", "true");
    intent.putExtra("scale", true);// 去黑边
    intent.putExtra("scaleUpIfNeeded", true);// 去黑边
    // aspectX aspectY 是宽高的比例
    intent.putExtra("aspectX", 4);//输出是X方向的比例
    intent.putExtra("aspectY", 3);
    intent.putExtra("outputX", 600);//输出X方向的像素
    intent.putExtra("outputY", 450);
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    intent.putExtra("noFaceDetection", true);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    intent.putExtra("return-data", false);//设置为不返回数据
    startActivityForResult(intent, REQUE_CODE_CROP);
  }

  /**
   * 7.0以上版本图片裁剪操作
   *
   * @param imagePath
   * @param REQUE_CODE_CROP
   */
  private void cropForN(String imagePath, int REQUE_CODE_CROP) {
    Uri cropUri = getImageContentUri(new File(imagePath));
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(cropUri, "image/*");
    intent.putExtra("crop", "true");
    //输出是X方向的比例
    intent.putExtra("aspectX", 4);
    intent.putExtra("aspectY", 3);
    // outputX outputY 是裁剪图片宽高
    intent.putExtra("outputX", 600);
    intent.putExtra("outputY", 450);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", false);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri);
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    intent.putExtra("noFaceDetection", true);
    startActivityForResult(intent, REQUE_CODE_CROP);
  }

  private Uri getImageContentUri(File imageFile) {
    String filePath = imageFile.getAbsolutePath();
    Cursor cursor = getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        new String[]{MediaStore.Images.Media._ID},
        MediaStore.Images.Media.DATA + "=? ",
        new String[]{filePath}, null);

    if (cursor != null && cursor.moveToFirst()) {
      int id = cursor.getInt(cursor
          .getColumnIndex(MediaStore.MediaColumns._ID));
      Uri baseUri = Uri.parse("content://media/external/images/media");
      return Uri.withAppendedPath(baseUri, "" + id);
    } else {
      if (imageFile.exists()) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DATA, filePath);
        return getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
      } else {
        return null;
      }
    }
  }

  /**
   * 安卓6.0以上版本权限处理
   */
  private void permissionForM() {
    if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
        Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
        Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

      ActivityCompat.requestPermissions(this,
          new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
          PERMISSIONS_FOR_TAKE_PHOTO);
    } else {
      takePictures();
    }
  }

  @Override
  public void onRequestPermissionsResult(int requestCode,
                      @NonNull String[] permissions, @NonNull int[] grantResults) {

    if (requestCode == PERMISSIONS_FOR_TAKE_PHOTO) {
      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        takePictures();
      }
      return;
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  }

}

–主界面布局——–




  

–AndroidManifest.xml添加provider——–

   
      
    

–资源文件下添加xml文件夹及file_paths文件——–



  
    
  

以上是“Android如何实现拍照及图片裁剪”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


网站题目:Android如何实现拍照及图片裁剪
地址分享:http://cdkjz.cn/article/popcop.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220