实训日记第一天_zPopuwindow照相机

/**

  • 通过这个 activity 启动的其他 Activity 返回的结果在这个方法进行处理 * 我们在这里对拍照、相册选择图片、裁剪图片的返回结果进行处理 * @param requestCode 返回码,用于确定是哪个 Activity 返回的数据 * @param resultCode 返回结果,一般如果操作成功返回的是 RESULT_OK * @param data 返回对应 activity 返回的数据
    */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
    // 通过返回码判断是哪个应用返回的数据
    switch (requestCode) {
    // / 拍照
    case TAKE_PHOTO_REQUEST_CODE:
    cropPhoto(photoUri);
    break;
    // 相册选择
    case CHOICE_FROM_ALBUM_REQUEST_CODE:
    cropPhoto(data.getData());
    break;
    // 裁剪图片
    case CROP_PHOTO_REQUEST_CODE:
    File file = new File(photoOutputUri.getPath());
    if (file.exists()) {
    Bitmap bitmap = BitmapFactory.decodeFile(photoOutputUri.getPath());
    img.setImageBitmap(bitmap); // file.delete(); // 选取完后删除照片
    } else {
    Toast.makeText(this, “找不到照片”, Toast.LENGTH_SHORT).show();
    }
    break;
    }
    }
    }

/**

  • 裁剪图片
    */
    private void cropPhoto(Uri inputUri) {
    // 调用系统裁剪图片的 Action
    Intent cropPhotoIntent = new Intent(“com.android.camera.action.CROP”);

    // 设置数据Uri 和类型 c
    cropPhotoIntent.setDataAndType(inputUri, “image/*”);
    // 授权应用读取 Uri,这一步要有,不然裁剪程序会崩溃
    cropPhotoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    // 设置图片的最终输出目录
    cropPhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoOutputUri = Uri.parse(“file:////sdcard/image_output.jpg”));
    startActivityForResult(cropPhotoIntent, CROP_PHOTO_REQUEST_CODE);
    }

/**

  • 从相册选取
    /
    private void choiceFromAlbum() {
    // 打开系统图库的 Action,等同于: “android.intent.action.GET_CONTENT”
    Intent choiceFromAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
    // 设置数据类型为图片类型
    choiceFromAlbumIntent.setType("image/
    ");
    startActivityForResult(choiceFromAlbumIntent, CHOICE_FROM_ALBUM_REQUEST_CODE);
    }

/** * 设置拍照得到的照片的储存目录,因为我们访问应用的缓存路径并不需要读写内存卡的申请权限, * 因此,这里为了方便,将拍照得到的照片存在这个缓存目录中 /
private void startCamera() {
]
File file = new File(getExternalCacheDir(), “image.jpg”);
try {
if (file.exists()) {
file.delete();
}
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
/
* * 因 Android 7.0 开始,不能使用 file:// 类型的 Uri 访问跨应用文件,否则报异常, * 因此我们这里需要使用内容提供器,FileProvider 是 ContentProvider 的一个子类, * 我们可以轻松的使用 FileProvider 来在不同程序之间分享数据(相对于 ContentProvider 来说) */
if (Build.VERSION.SDK_INT >= 24) {
photoUri = FileProvider.getUriForFile(this, “com.jiyun.zuoye”, file);
} else {
photoUri = Uri.fromFile(file); // Android 7.0 以前使用原来的方法来获取文件的 Uri }
}
// 打开系统相机的 Action,等同于:“android.media.action.IMAGE_CAPTURE”
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 设置拍照所得照片的输出目录
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST_CODE);
}

//打开照相机
private void CAERA() {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
startCamera();

}

授权及相册选取 方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {

    case 0:

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            CAERA();
        } else {

            Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
        }

        break;
    // 打开相册选取:
    case 1:

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        } else {
            Toast.makeText(this, "读写内存卡内容权限被拒绝", Toast.LENGTH_SHORT).show();
        }

        break;

}

}

popuwindow按钮监听_从相册选取
choiceFromAlbum();

popuwindow按钮监听_拍照

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 0);
} else {

CAERA();

}

popuwindow布局

实训日记第一天_zPopuwindow照相机

动态授予权限
实训日记第一天_zPopuwindow照相机

实训日记第一天_zPopuwindow照相机

窗口popuwindow定义实训日记第一天_zPopuwindow照相机

定义变量
实训日记第一天_zPopuwindow照相机