Android examples for Camera:Photo
Tries to create photo placeholder and launch camera app
//package com.java2s; import android.app.Activity; import android.app.Fragment; import android.content.ActivityNotFoundException; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import java.io.File; public class Main { private static final String[] PROJECTION = new String[] { MediaStore.MediaColumns.DATA }; private static final ContentValues CONTENT_VALUES = new ContentValues(1); private static final String PHOTO_SHARED_PREFS_NAME = "photo_shared"; private static final String PHOTO_URI = "photo_uri"; /**// w ww.j ava 2 s . co m * Tries to create photo placeholder and launch camera app * @param requestCode your unique code that will be returned in onActivityResult * @param caller caller Activity * @param photoKey key in Shared Preferences for taken image, can be null * @throws android.content.ActivityNotFoundException if no camera app was found * @throws Exception if there is a problem with create photo eg. SDcard is not mounted. It may be eg. {@link IllegalStateException} or {@link UnsupportedOperationException} depending on {@link ContentResolver}. */ public static void launchCameraApp(int requestCode, Activity caller, String photoKey) throws Exception { if (photoKey == null) { photoKey = PHOTO_URI; } ContentResolver cr = caller.getContentResolver(); Uri takenPhotoUri = cr.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, CONTENT_VALUES); if (takenPhotoUri == null) { throw new IllegalStateException("Photo insertion failed"); } Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, getIntentUri(takenPhotoUri, cr)); caller.startActivityForResult(intent, requestCode); SharedPreferences prefs = caller.getSharedPreferences( PHOTO_SHARED_PREFS_NAME, Context.MODE_PRIVATE); prefs.edit().putString(photoKey, takenPhotoUri.toString()).commit(); } public static void launchCameraApp(int requestCode, Fragment caller, String photoKey) throws Exception { if (photoKey == null) { photoKey = PHOTO_URI; } Activity activity = caller.getActivity(); if (activity != null) { ContentResolver cr = caller.getActivity().getContentResolver(); Uri takenPhotoUri = cr.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, CONTENT_VALUES); if (takenPhotoUri == null) { throw new IllegalStateException("Photo insertion failed"); } Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, getIntentUri(takenPhotoUri, cr)); caller.startActivityForResult(intent, requestCode); SharedPreferences prefs = caller.getActivity() .getSharedPreferences(PHOTO_SHARED_PREFS_NAME, Context.MODE_PRIVATE); prefs.edit().putString(photoKey, takenPhotoUri.toString()) .commit(); } else { throw new ActivityNotFoundException(); } } private static Uri getIntentUri(Uri takenPhotoUri, ContentResolver cr) { String path = getPhotoFilePath(takenPhotoUri, cr); if (path == null) { throw new IllegalStateException("Photo resolution failed"); } return Uri.fromFile(new File(path)); } private static String getPhotoFilePath(Uri takenPhotoUri, ContentResolver cr) { Cursor cursor = cr.query(takenPhotoUri, PROJECTION, null, null, null); String res = null; if (cursor != null) { int dataIdx = cursor .getColumnIndex(MediaStore.MediaColumns.DATA); if (dataIdx >= 0 && cursor.moveToFirst()) res = cursor.getString(dataIdx); cursor.close(); } return res; } }