Android中相机,相册,剪切的原生使用

零.使用的路径

String path = Environment.getExternalStorageDirectory() + "/head.jpg";

一.首先添加权限


    
    
    


二.调用相机

// 这个出捕获图片的常量值
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                // 设置图片输出位置; 输出到制定的uri路径上;
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
                // 设置请求码
                startActivityForResult(intent, 100);
三.调用相册


Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 101);

四.剪切的方法


/**
     * 剪切
     */
    public void crop(Uri uri) {
        // 隐式启动, 里面传的是action
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // 裁剪框的比例,1:1
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);
        intent.putExtra("outputFormat", "JPEG");// 图片格式
        intent.putExtra("noFaceDetection", false);// 取消人脸识别
        // 设置剪裁后是否返回数据
        intent.putExtra("return-data", true);
        // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT
        startActivityForResult(intent, 102);

    }



五.返回调用

@Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("resultCode:---", resultCode + "");
        if (requestCode == 100 && resultCode == RESULT_OK) {
//          iv.setImageURI(Uri.fromFile(new File(path)));
//            crop(Uri.fromFile(new File(path)));
            uploadFiles(new File(path));
        } else if (requestCode == 101 && resultCode == RESULT_OK) {
            // 获取data中的数据
            Uri uri = data.getData();
//         iv.setImageURI(uri);
//            crop(uri);
            String[] proj = { MediaStore.Images.Media.DATA };

            Cursor actualimagecursor = managedQuery(uri,proj,null,null,null);

            int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            actualimagecursor.moveToFirst();

            String img_path = actualimagecursor.getString(actual_image_column_index);

            File file = new File(img_path);
            Log.e("MainActivity","URL:"+uri.toString());
            uploadFiles(file);
        } else if (requestCode == 102 && resultCode == RESULT_OK) {
            //这个参数data,是固定写法
            File file=new File(path);
            Bitmap bitmap = data.getParcelableExtra("data");
            try {
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //上传
            uploadFiles(file);
        }







你可能感兴趣的:(android)