【发布时间】:2021-07-06 10:08:31
【问题描述】:
我在 google 上搜索 onactivityresult() 已弃用。但我的问题没有解决。这是片段的代码
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull int[] grantResults) {
switch (requestCode){
case CAMERA_REQUEST_CODE:{
if (grantResults.length >0 ){
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean writeStorageAccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (cameraAccepted && writeStorageAccepted){
pickFromCamera();
}
else {
Toast.makeText(getActivity(), "Please enable camera & storage permission first ", Toast.LENGTH_SHORT).show();
}
}
}
private void pickFromCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE,"Temp Pic");
values.put(MediaStore.Images.Media.DESCRIPTION,"Temp Description");
// put image uri
image_uri = requireActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
// intent tom start camera
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(cameraIntent, IMAGE_PICK_CAMERA_CODE);
}
private void pickFromGallery() {
// pick from gallery
Intent galleryIntent = new Intent(Intent .ACTION_PICK);
galleryIntent.setType("Images/*");
startActivityForResult(galleryIntent, IMAGE_PICK_GALLERY_CODE);
}
}
【问题讨论】:
-
stackoverflow.com/questions/62671106/… 这正是您要找的。span>
标签: java android deprecated