android开发——获取相机或者相册图片并裁剪图片

实现思路:裁剪图片是一个很常用的功能,早就有人写好了的。所以应该能找到相关的开源项目。

于是GitHub上有了:https://github.com/jdamcd/android-crop

使用步骤如下:

1、将github上的项目拷贝下来。

2、将 开源项目的lib拷贝到你项目上(和app同级目录)

3、在setting.gradle中包含这个模块:include ':app',':lib'

4、在app.gradle中添加依赖:

dependencies {
    implementation project(':lib')
}

5、在AndroidMenifest.xml中添加这个Activity:(相关权限自行配置)

<activity android:name="com.soundcloud.android.crop.CropImageActivity" />

6、在Activity中完成调用。

private int SELECT_PICTURE = 0;
private int SELECT_CAMERA = 1;
public final static int CONSULT_DOC_PICTURE = 1000;
public final static int CONSULT_DOC_CAMERA = 1001;
public final static int CORP_CAMERA_IMAGE = 1002;
private Uri cameraFileUri;

//选择图库或者相机
protected void createSelectImageDialog(){
        CharSequence[] items = {"相册","照相机"};
        new AlertDialog.Builder(this).setTitle("选择图片来源").setItems(items, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                if(which == SELECT_PICTURE){
                    Crop.pickImage(MainActivity.this,CONSULT_DOC_PICTURE);
                }else{
                    File file = new File(getFileSavePath(MainActivity.this),System.currentTimeMillis()+".jpg");
                    cameraFileUri = Uri.fromFile(file);
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,cameraFileUri);
                    startActivityForResult(intent, CONSULT_DOC_CAMERA);
                }
            }
        }).create().show();
}

//打开相机、图库或者截图的回调方法
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CONSULT_DOC_PICTURE) {
            beginCrop(data.getData());
        } else if (requestCode == CONSULT_DOC_CAMERA && resultCode == Activity.RESULT_OK) 
        {
            try {
                Uri outputUri = Uri.fromFile(File.createTempFile("corp", ".jpg"));
                Crop.of(cameraFileUri, outputUri).asSquare()
                        .start(this, CORP_CAMERA_IMAGE);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if(requestCode == CORP_CAMERA_IMAGE){
            if(data !=null){
                Uri corpImageUri = Crop.getOutput(data);
                imageView.setImageURI(corpImageUri);
            }else{
                Toast.makeText(this, "请重新选择图片", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "请重新选择图片", Toast.LENGTH_SHORT).show();
        }
    }

    //开始截图
    private void beginCrop(Uri source) {
        Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
        Crop.of(source, destination).asSquare().start(this,CORP_CAMERA_IMAGE);
    }

   /**
     * 获取手机储存路径或者运行内存路径
     * @param ctx 上下文环境
     * @return
     */
    public static String getFileSavePath(Context ctx) {
        String path = null;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            path = Environment.getExternalStorageDirectory().getAbsolutePath();
        } else if (Environment.getDataDirectory() != null) {
            // 读取手机内存路径
            File innerfile = Environment.getDataDirectory();
            StatFs stf = new StatFs(innerfile.getPath());
            long blocksize = stf.getBlockSize();
            long avilableblocks = stf.getAvailableBlocks();
            long sizeM = blocksize * avilableblocks / 1024 / 1024;
            if (sizeM > 10) {
                path = innerfile.getAbsolutePath();
            }
        }
        return path;
    }

注意事项: 

1、android 7.0打开相机闪退的话,在Application的onCreate()方法中添加如下代码:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();

2、打开相机并截图,会在手机内置存储根目录保存截图前的图片,请自行处理。

  如果对您有用的话赞一下呗!谢谢谢谢~

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值