Magic Picker is a simple library to get rid of Android Permissions while picking image through gallery or camera.
All the permission related code for accessing camera or gallery is handled by library internally, so that you can focus on important stuff 😉
- Open Module Settings -> Click on Add New Module -> Select Import Gradle project and select picker library.
- Open app level build.gradle and add following line in dependencies and you are good to go 😃
compile project(':picker')
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
Create xml resource folder in values folder and create a file provider_paths.xml inside it.
Add following code in your AndroidManifest.xml file inside -application- tag.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
To invoke Gallery or Camera, you need to pass custom intent to startActivityForResult function. With Magic Picker, you can do this with just two lines of code.
For Gallery
Intent intent = new PickerBuilder(context).openGallery(true).build();
startActivityForResult(intent, ImageContants.PICK_IMAGE);
For Camera
Intent intent = new PickerBuilder(context).openCamera(true).build();
startActivityForResult(intent, ImageContants.PICK_IMAGE);
To get Image Uri, implement onActivityResult method inside your Activity or Fragment and get the Uri by calling data.getData()
Example
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case ImageContants.PICK_IMAGE:
Glide.with(context).load(data.getData()).into(imageview);
break;
}
}
}