Skip to content

Commit

Permalink
Merge branch 'release/1.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariovc committed Jan 13, 2017
2 parents 832e352 + b4f959b commit 18bf1a3
Showing 1 changed file with 64 additions and 12 deletions.
76 changes: 64 additions & 12 deletions library/src/main/java/com/mvc/imagepicker/ImagePicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package com.mvc.imagepicker;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
Expand All @@ -27,11 +30,14 @@
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -111,11 +117,19 @@ public static Intent getPickImageIntent(Context context, String chooserTitle) {

Intent pickIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotoIntent.putExtra("return-data", true);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTemporalFile(context)));
intentList = addIntentsToList(context, intentList, pickIntent);
intentList = addIntentsToList(context, intentList, takePhotoIntent);

// Camera action will fail if the app does not have permission, check before adding intent.
// We only need to add the camera intent if the app does not use the CAMERA permission
// in the androidmanifest.xml
// Or if the user has granted access to the camera.
// See https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE
if (!appManifestContainsPermission(context, Manifest.permission.CAMERA) || hasCameraAccess(context)) {
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotoIntent.putExtra("return-data", true);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTemporalFile(context)));
intentList = addIntentsToList(context, intentList, takePhotoIntent);
}

if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
Expand All @@ -140,6 +154,42 @@ private static List<Intent> addIntentsToList(Context context, List<Intent> list,
return list;
}

/**
* Checks if the current context has permission to access the camera.
* @param context context.
*/
private static boolean hasCameraAccess(Context context) {
return ContextCompat.checkSelfPermission(context,
Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
}

/**
* Checks if the androidmanifest.xml contains the given permission.
* @param context context.
* @return Boolean, indicating if the permission is present.
*/
private static boolean appManifestContainsPermission(Context context, String permission) {
PackageManager pm = context.getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = null;
if (packageInfo != null) {
requestedPermissions = packageInfo.requestedPermissions;
}
if (requestedPermissions == null) {
return false;
}

if (requestedPermissions.length > 0) {
List<String> requestedPermissionsList = Arrays.asList(requestedPermissions);
return requestedPermissionsList.contains(permission);
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return false;
}

/**
* Called after launching the picker with the same values of Activity.getImageFromResult
* in order to resolve the result and get the image.
Expand Down Expand Up @@ -203,16 +253,18 @@ private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize)

try {
fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}

if (fileDescriptor != null) {
actuallyUsableBitmap = BitmapFactory
.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
Log.i(TAG, "Trying sample size " + options.inSampleSize + "\t\t"
+ "Bitmap width: " + actuallyUsableBitmap.getWidth()
+ "\theight: " + actuallyUsableBitmap.getHeight());
if (actuallyUsableBitmap != null) {
Log.i(TAG, "Trying sample size " + options.inSampleSize + "\t\t"
+ "Bitmap width: " + actuallyUsableBitmap.getWidth()
+ "\theight: " + actuallyUsableBitmap.getHeight());
}
fileDescriptor.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return actuallyUsableBitmap;
Expand Down

0 comments on commit 18bf1a3

Please sign in to comment.