I have added a button in my activity and I want that when I click on the button it should access for runtime permission to access device photos. Some how it automatically denies the permission and the app crashes. Can someone explain me the logic or give me some code for it?
I gave permission in the manifest file and wrote the logic on what to do if permission denied. My code: ‘ private ImageView imageViewSettings;
private int permissionDeniedCount = 0;
private ActivityResultLauncher<String> requestPermissionLauncher;
private ActivityResultLauncher<Intent> pickImageLauncher;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
imageViewSettings = view.findViewById(R.id.imageViewSettings);
Button buttonAccessPhotos = view.findViewById(R.id.buttonAccessPhotos);
requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
Log.d(TAG, "Permission result: " + isGranted);
if (isGranted) {
Toast.makeText(getContext(), "Permission allowed", Toast.LENGTH_SHORT).show();
accessPhotos();
} else {
permissionDeniedCount++;
if (permissionDeniedCount >= 2) {
// Show toast and guide the user to the app settings
Toast.makeText(getContext(), "Permission denied twice. Please enable from settings.", Toast.LENGTH_LONG).show();
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getContext().getPackageName()));
startActivity(intent);
} else {
Toast.makeText(getContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
}
});
pickImageLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == FragmentActivity.RESULT_OK && result.getData() != null && result.getData().getData() != null) {
Uri selectedImage = result.getData().getData();
imageViewSettings.setImageURI(selectedImage);
}
});
buttonAccessPhotos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Access Photos button clicked");
checkPermission();
}
});
return view;
}
private void checkPermission() {
FragmentActivity activity = getActivity();
if (activity == null) {
Log.e(TAG, "Activity is null");
return;
}
int permissionState = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
Log.d(TAG, "Permission state: " + permissionState);
if (permissionState != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
Log.d(TAG, "Showing permission rationale");
Toast.makeText(getContext(), "We need access to your photos to select an image.", Toast.LENGTH_LONG).show();
requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
} else {
if (permissionDeniedCount >= 2) {
Log.d(TAG, "Permission denied permanently. Guiding user to settings.");
Toast.makeText(getContext(), "Permission denied twice. Please enable from settings.", Toast.LENGTH_LONG).show();
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getContext().getPackageName()));
startActivity(intent);
} else {
Log.d(TAG, "Requesting permission");
requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
}
}
} else {
// Permission has already been granted
Log.d(TAG, "Permission already granted");
accessPhotos();
}
}
private void accessPhotos() {
Log.d(TAG, "Accessing photos");
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickImageLauncher.launch(intent);
}'
MAHARSH RAVAL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.