Android访问手机相机和图库

访问相机

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
          startActivityForResult(intent,requestCode);

访问图库

Intent intent = new Intent(Intent.ACTION_PICK, null);                      
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*";
startActivityForResult(intent, requestCode);

访问图库剪切器

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
 // outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 138);
intent.putExtra("outputY", 138);
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT,crop_uri);
startActivityForResult(intent,requestCode);

得到访问结果

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode){
                case requestCode://拍照获取
                 
                    break;
                case requestCode ://相册获取
                 
                    break;
                case requestCode://裁剪返回结果
                  
                    break;
        }
    }

定义图片存放路径

//File.separator 文件分隔器,windows是 \  unix是 /
//Constants.PROJECT_FOLDER 存放在内存中的包名

path = Environment.getExternalStorageDirectory() + File.separator + Constants.PROJECT_FOLDER + "/upload/"+"temp.jpg"
Uri uri = Uri.parse("file://" + path);

你可能感兴趣的:(Android访问手机相机和图库)