Skip to content

Commit 4fcda03

Browse files
committed
Handled permissions permanently denied case
1 parent eca6a26 commit 4fcda03

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

README.MD

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ Android Choosing Image from Camera or Gallery with Crop Functionality
22
===================
33
Android sample project demonstrating choosing an image from gallery or camera with the cropping functionality.
44

5+
![Demo](https://www.androidhive.info/wp-content/uploads/2019/02/android-image-from-gallery-or-camera-with-crop-min.jpg)
6+
57
| [Complete Tutorial](https://www.androidhive.info/2018/01/android-content-placeholder-animation-like-facebook-using-shimmer/#disqus_thread) | [Apk](http://download.androidhive.info/apk/shimmer.apk) | [Video Demo](https://www.youtube.com/watch?v=j-TPBl39rDk)|
68
|----------|--------|------|
79

8-
![Demo](https://www.androidhive.info/wp-content/uploads/2019/02/android-image-from-gallery-or-camera-with-crop-min.jpg)
9-
1010
uCrop
1111
---
1212
Thanks to [Yalantis](https://github.com/Yalantis) for providing such a beautiful cropping ([uCrop](https://github.com/Yalantis/uCrop)) library. This example uses the uCrop library for cropping functionality.
@@ -203,4 +203,14 @@ Intent Parameters:
203203
| INTENT_BITMAP_MAX_WIDTH | The maximum width of the cropped image |
204204
| INTENT_BITMAP_MAX_HEIGHT | The maximum height of the cropped image |
205205

206-
If you want additional options, you can customize the image picker activity.
206+
If you want additional options, you can customize the image picker activity.
207+
208+
Deleting Cached Images:
209+
==========
210+
While the image are taken with camera, they will stored in cached directory. You can clear the cached images once the bitmap is utilized.
211+
```java
212+
// Clearing older images from cache directory
213+
// don't call this line if you want to choose multiple images in the same activity
214+
// call this once the bitmap(s) usage is over
215+
ImagePickerActivity.clearCache(this);
216+
```

app/src/main/java/info/androidhive/imagepicker/MainActivity.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import android.Manifest;
44
import android.app.Activity;
5+
import android.content.DialogInterface;
56
import android.content.Intent;
67
import android.graphics.Bitmap;
78
import android.net.Uri;
89
import android.os.Bundle;
910
import android.provider.MediaStore;
11+
import android.provider.Settings;
1012
import android.support.annotation.Nullable;
1113
import android.support.v4.content.ContextCompat;
14+
import android.support.v7.app.AlertDialog;
1215
import android.support.v7.app.AppCompatActivity;
1316
import android.support.v7.widget.Toolbar;
1417
import android.util.Log;
@@ -45,6 +48,10 @@ protected void onCreate(Bundle savedInstanceState) {
4548
getSupportActionBar().setTitle(null);
4649

4750
loadProfileDefault();
51+
52+
// Clearing older images from cache directory
53+
// don't call this line if you want to choose multiple images in the same activity
54+
// call this once the bitmap(s) usage is over
4855
ImagePickerActivity.clearCache(this);
4956
}
5057

@@ -71,8 +78,10 @@ void onProfileImageClick() {
7178
public void onPermissionsChecked(MultiplePermissionsReport report) {
7279
if (report.areAllPermissionsGranted()) {
7380
showImagePickerOptions();
74-
} else {
75-
// TODO - handle permission denied case
81+
}
82+
83+
if (report.isAnyPermissionPermanentlyDenied()) {
84+
showSettingsDialog();
7685
}
7786
}
7887

@@ -142,4 +151,30 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
142151
}
143152
}
144153
}
154+
155+
/**
156+
* Showing Alert Dialog with Settings option
157+
* Navigates user to app settings
158+
* NOTE: Keep proper title and message depending on your app
159+
*/
160+
private void showSettingsDialog() {
161+
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
162+
builder.setTitle(getString(R.string.dialog_permission_title));
163+
builder.setMessage(getString(R.string.dialog_permission_message));
164+
builder.setPositiveButton(getString(R.string.go_to_settings), (dialog, which) -> {
165+
dialog.cancel();
166+
openSettings();
167+
});
168+
builder.setNegativeButton(getString(android.R.string.cancel), (dialog, which) -> dialog.cancel());
169+
builder.show();
170+
171+
}
172+
173+
// navigating user to app settings
174+
private void openSettings() {
175+
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
176+
Uri uri = Uri.fromParts("package", getPackageName(), null);
177+
intent.setData(uri);
178+
startActivityForResult(intent, 101);
179+
}
145180
}

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
<string name="font_family_condensed">sans-serif-condensed</string>
2020
<string name="font_family_black">sans-serif-black</string>
2121
<string name="font_family_thin">sans-serif-thin</string>
22+
<string name="dialog_permission_title">Grant Permissions</string>
23+
<string name="dialog_permission_message">This app needs permission to use this feature. You can grant them in app settings.</string>
24+
<string name="go_to_settings">GOTO SETTINGS</string>
2225
</resources>

0 commit comments

Comments
 (0)