|
| 1 | +package com.lwp.xiaoyun_core.ui.camera; |
| 2 | + |
| 3 | +import android.content.ContentValues; |
| 4 | +import android.content.Intent; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.drawable.ColorDrawable; |
| 7 | +import android.net.Uri; |
| 8 | +import android.os.Build; |
| 9 | +import android.provider.MediaStore; |
| 10 | +import android.support.v7.app.AlertDialog; |
| 11 | +import android.view.Gravity; |
| 12 | +import android.view.View; |
| 13 | +import android.view.Window; |
| 14 | +import android.view.WindowManager; |
| 15 | + |
| 16 | +import com.blankj.utilcode.util.FileUtils; |
| 17 | +import com.lwp.xiaoyun_core.R; |
| 18 | +import com.lwp.xiaoyun_core.delegates.PermissionCheckerDelegate; |
| 19 | +import com.lwp.xiaoyun_core.util.file.FileUtil; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | + |
| 23 | +/** |
| 24 | + * <pre> |
| 25 | + * author : 李蔚蓬(简书_凌川江雪) |
| 26 | + * time : 2020/3/2 5:36 |
| 27 | + * desc : 照片处理类 |
| 28 | + * </pre> |
| 29 | + */ |
| 30 | +public class CameraHandler implements View.OnClickListener { |
| 31 | + |
| 32 | + private final AlertDialog DIALOG; |
| 33 | + private final PermissionCheckerDelegate DELEGATE; |
| 34 | + |
| 35 | + public CameraHandler(PermissionCheckerDelegate delegate) { |
| 36 | + this.DELEGATE = delegate; |
| 37 | + DIALOG = new AlertDialog.Builder(delegate.getContext()).create(); |
| 38 | + } |
| 39 | + |
| 40 | + //弹出Dialog弹框 内容:三个按钮,拍照 选图 取消 |
| 41 | + final void beginCameraDialog() { |
| 42 | + DIALOG.show(); |
| 43 | + |
| 44 | + final Window window = DIALOG.getWindow(); |
| 45 | + if (window != null) { |
| 46 | + window.setContentView(R.layout.dialog_camera_panel);//设置弹框布局 |
| 47 | + window.setGravity(Gravity.BOTTOM); |
| 48 | + window.setWindowAnimations(R.style.anim_panel_up_from_bottom); |
| 49 | + window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
| 50 | + |
| 51 | + //设置属性 |
| 52 | + final WindowManager.LayoutParams params = window.getAttributes(); |
| 53 | + params.width = WindowManager.LayoutParams.MATCH_PARENT; |
| 54 | + params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND; |
| 55 | + params.dimAmount = 0.5f;//设置幕布黑暗程度 |
| 56 | + window.setAttributes(params); |
| 57 | + |
| 58 | + //设置弹框中 组件的监听 |
| 59 | + window.findViewById(R.id.photodialog_btn_cancel).setOnClickListener(this);//取消按钮 |
| 60 | + window.findViewById(R.id.photodialog_btn_take).setOnClickListener(this);//拍照按钮 |
| 61 | + window.findViewById(R.id.photodialog_btn_native).setOnClickListener(this);//本地按钮 |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private String getPhotoName() { |
| 66 | + //获取一个 模板格式化后的 文件名(模板:文件头_当前时间.后缀) |
| 67 | + return FileUtil.getFileNameByTime("IMG", "jpg"); |
| 68 | + } |
| 69 | + //拍照取图 |
| 70 | + public void takePhoto() { |
| 71 | + final String currentPhotoName = getPhotoName(); |
| 72 | + //拍照意图 |
| 73 | + final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 74 | + //File 临时文件句柄 临时文件:这里是系统相册目录下的当前文件名的文件临时句柄 |
| 75 | + //CAMERA_PHOTO_DIR 系统相册目录 |
| 76 | + final File tempFile = new File(FileUtil.CAMERA_PHOTO_DIR, currentPhotoName); |
| 77 | + |
| 78 | + //兼容7.0及以上的写法 |
| 79 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 80 | + final ContentValues contentValues = new ContentValues(1); |
| 81 | + contentValues.put(MediaStore.Images.Media.DATA, tempFile.getPath()); |
| 82 | + //使用 ContentProvider 的方式 |
| 83 | + final Uri uri = DELEGATE.getContext().getContentResolver(). |
| 84 | + insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); |
| 85 | + |
| 86 | + //需要讲Uri路径转化为实际路径 |
| 87 | + final File realFile = |
| 88 | + FileUtils.getFileByPath(FileUtil.getRealFilePath(DELEGATE.getContext(), uri)); |
| 89 | + |
| 90 | + final Uri realUri = Uri.fromFile(realFile); |
| 91 | + CameraImageBean.getInstance().setPath(realUri); |
| 92 | + intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); |
| 93 | + } else { |
| 94 | + |
| 95 | + final Uri fileUri = Uri.fromFile(tempFile); |
| 96 | + CameraImageBean.getInstance().setPath(fileUri); |
| 97 | + intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); |
| 98 | + } |
| 99 | + |
| 100 | + //使用startActivityForResult()的形式 启动Activity |
| 101 | + DELEGATE.startActivityForResult(intent, RequestCodes.TAKE_PHOTO); |
| 102 | + } |
| 103 | + |
| 104 | + //本地取图 |
| 105 | + private void pickPhoto() { |
| 106 | + final Intent intent = new Intent(); |
| 107 | + intent.setType("image/*");//所有的Image类型 |
| 108 | + intent.setAction(Intent.ACTION_GET_CONTENT);//获取内容 |
| 109 | + intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 110 | + |
| 111 | + //使用startActivityForResult()的形式 启动Activity |
| 112 | + // createChooser 创建选择器 |
| 113 | + DELEGATE.startActivityForResult |
| 114 | + (Intent.createChooser(intent, "选择获取图片的方式"), RequestCodes.PICK_PHOTO); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + @Override |
| 119 | + public void onClick(View v) { |
| 120 | + int id = v.getId(); |
| 121 | + |
| 122 | + if (id == R.id.photodialog_btn_cancel) { |
| 123 | + DIALOG.cancel(); |
| 124 | + } else if (id == R.id.photodialog_btn_take) { |
| 125 | + //拍照取图 |
| 126 | + takePhoto(); |
| 127 | + DIALOG.cancel(); |
| 128 | + } else if (id == R.id.photodialog_btn_native) { |
| 129 | + //本地取图 |
| 130 | + pickPhoto(); |
| 131 | + DIALOG.cancel(); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments