Last active
March 23, 2017 02:22
-
-
Save sreejithraman/237365083a7e83d741b2e59c012b341b to your computer and use it in GitHub Desktop.
The Missing Documentation: Camera Intents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void startCameraIntent() { | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
// Ensure that there's a camera activity to handle the intent | |
if (takePictureIntent.resolveActivity(getPackageManager()) != null) { | |
try { | |
imageFile = createImageFile(); | |
} catch (IOException e) { | |
// file wasn't created | |
} | |
if (imageFile != null) { | |
Uri imageUri = FileProvider.getUriForFile( | |
ExampleActivity.this, | |
BuildConfig.APPLICATION_ID + ".fileprovider", | |
imageFile); | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); | |
// This is important. Without it, you may get Security Exceptions. | |
// Google fails to mention this in their docs... | |
// Taken from: https://github.com/commonsguy/cw-omnibus/blob/master/Camera/FileProvider/app/src/main/java/com/commonsware/android/camcon/MainActivity.java | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); | |
} | |
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | |
ClipData clip = ClipData.newUri(getContentResolver(), "A photo", imageUri); | |
intent.setClipData(clip); | |
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); | |
} | |
else { | |
List<ResolveInfo> resInfoList = | |
getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); | |
for (ResolveInfo resolveInfo : resInfoList) { | |
String packageName = resolveInfo.activityInfo.packageName; | |
grantUriPermission(packageName, imageUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION); | |
} | |
} | |
} | |
startActivityForResult(intent, REQUEST_CODE_CAMERA); | |
} | |
else { | |
// device doesn't have camera | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey, buddy. I found a mistake!
At Line 2 the name of Intent Object is intent , but Line 5 is the takePictureIntent.
So they are mismatching.