Skip to content

Instantly share code, notes, and snippets.

@sreejithraman
sreejithraman / CreateImage.java
Last active March 20, 2017 14:39
The Missing Documentation: Camera Intents
File imageFile; // use this to keep a reference to the file you create so that we can access it from onActivityResult()
private void createImageFile() throws IOException {
String imageFileName = "image" + System.currentTimeMillis() + "_"; // give it a unique filename
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
// use this if you want android to automatically save it into the device's image gallery:
// File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
}
@sreejithraman
sreejithraman / AndroidManifest.xml
Last active March 21, 2017 00:55
The Missing Documentation: Camera Intents
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<!-- that should be android:maxSdkVersion="18" ended up having this issue:
http://stackoverflow.com/questions/31831453/getexternalfilesdir-fail -->
<!--- If you want to save to the public image directory (the image gallery), you need to do this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
--->
...
@sreejithraman
sreejithraman / FaceDetectionExample.java
Created March 16, 2017 21:52
The Missing Documentation: Using Face Detector with Files
int rotateAngle;
void rotatePhoto(){
rotateAngle += 90f;
if (rotateAngle == 360f) {
rotateAngle = 0;
}
photoImageView.setRotation(rotateAngle); // just rotate the image view for the user
}
@sreejithraman
sreejithraman / StartCameraIntent.java
Last active March 23, 2017 02:22
The Missing Documentation: Camera Intents
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
}
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="{your.app.package}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // note that data will be null
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CODE_CAMERA) {
// imageFile will have the photo in it so do whatever you want with it
}
}
}