Android开发不用存储权限进行拍照
有时平台审核严,拍照时不能获取存储。这怎么办呢,拍照功能又不能去掉。
一、思路:
得自定义Camera,然后把拍照图片存在app目录下,再刷新到手机相册
二、效果图:
Android开发教程实战案例源码分享-不用存储权限进行拍照
三、关键代码如下:
// 联系:893151960
public class SecondActivity extends AppCompatActivity {
private ImageView iv_result;
private Context mContext;
private ActivityResultLauncher<Intent> launcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
String photoPath = result.getData().getStringExtra(CameraActivity.KEY_CONTENT_TYPE);
Log.i("photoPath",photoPath);
if (!TextUtils.isEmpty(photoPath)) {
Glide.with(mContext)
.load(photoPath)
.into(iv_result);
}
}
});
iv_result = findViewById(R.id.iv_result);
findViewById(R.id.tv_photo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermission(mContext, new View.OnClickListener() {
@Override
public void onClick(View view) {
startCameraActivity(mContext,CameraActivity.CONTENT_TYPE_GENERAL,launcher);
}
}, new String[]{
Manifest.permission.CAMERA,
} );
}
});
}
/**
* 身份证取景拍照
*/
public static void startCameraActivity(Context mContext,String type, ActivityResultLauncher<Intent> launcher) {
Intent intent = new Intent(mContext, CameraActivity.class);
//intent.putExtra(CameraActivity.KEY_CONTENT_TYPE,CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
intent.putExtra(CameraActivity.KEY_CONTENT_TYPE,type);
launcher.launch(intent);
}
public static void requestPermission(final Context context, final View.OnClickListener successListener, String... permissions) {
if (PermissionCheckUtil.checkPermissions(context,permissions)){
if (null != successListener) {
successListener.onClick(null);
}
} else {
PermissionAppUtils.INSTANCE.requestPermission(context, permissions, new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Throwable {
if (aBoolean){
if (null != successListener) {
successListener.onClick(null);
}
}
}
});
}
}
}
四、完整项目demo结构图:
有问题或者需要源码私信我