Skip to content

Commit

Permalink
Merge pull request #29 from percolate/fix_camera_uri_intent_crash
Browse files Browse the repository at this point in the history
Fix crash in IntentUtils#launchCameraIntent on pre-lollipop devices
  • Loading branch information
brentwatson authored Mar 14, 2017
2 parents e028809 + 4e0c6b2 commit e2265ca
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
10 changes: 5 additions & 5 deletions caffeine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ group 'com.percolate.caffeine'
version '1.0-SNAPSHOT'

android {
compileSdkVersion 24
buildToolsVersion = "24.0.3"
compileSdkVersion 25
buildToolsVersion = '25.0.2'

buildTypes {
debug {
Expand All @@ -17,9 +17,9 @@ android {

defaultConfig {
minSdkVersion 14
targetSdkVersion 24
versionCode 41
versionName "0.4.2"
targetSdkVersion 25
versionCode 43
versionName "0.4.3"
}

packagingOptions {
Expand Down
19 changes: 19 additions & 0 deletions caffeine/src/main/java/com/percolate/caffeine/IntentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import java.util.List;

/**
* Utility methods for starting various kinds of system {@link android.content.Intent}'s.
*/
Expand Down Expand Up @@ -64,10 +68,25 @@ public static void launchCameraIntent(final Activity context, final Uri outputDe
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputDestination);
if (intent.resolveActivity(context.getPackageManager()) != null) {
grantUriPermissionsForIntent(context, outputDestination, intent);
context.startActivityForResult(intent, requestCode);
}
}

/**
* Grant permissions to read/write the given URI.
* Take from: http://stackoverflow.com/a/33754937/1234900
*/
private static void grantUriPermissionsForIntent(final Activity context, final Uri outputDestination, final Intent intent) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
final List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
final String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, outputDestination, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
}

/**
* Tries to launch the application for a given package name.
* @return true if launch succeeded, otherwise false.
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Sep 15 13:53:34 EDT 2016
#Fri Mar 10 13:45:53 EST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6 changes: 3 additions & 3 deletions tests/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion = "24.0.3"
compileSdkVersion 25
buildToolsVersion = "25.0.2"

defaultConfig {
applicationId "com.percolate.caffeine.testapp"
minSdkVersion 14
targetSdkVersion 24
targetSdkVersion 25
versionCode 687
versionName "5.4.8"
}
Expand Down

0 comments on commit e2265ca

Please sign in to comment.