Android examples for App:Local Storage
Tries to obtain File containing taken photo.
//package com.java2s; import android.content.ContentResolver; import android.content.Context; 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 String PHOTO_SHARED_PREFS_NAME = "photo_shared"; private static final String PHOTO_URI = "photo_uri"; /**/* w w w .j a v a 2 s .c o m*/ * Tries to obtain File containing taken photo. Perform cleanups if photo was not taken or it is empty. * @param caller caller Activity * @param photoKey key in Shared Preferences for taken image, can be null * @return File containing photo or null if no or empty photo was saved by camera app. */ public static File retrievePhotoResult(Context caller, String photoKey) { try { if (photoKey == null) { photoKey = PHOTO_URI; } SharedPreferences prefs = caller.getSharedPreferences( PHOTO_SHARED_PREFS_NAME, Context.MODE_PRIVATE); String takenPhotoUriString = prefs.getString(photoKey, null); prefs.edit().remove(photoKey).commit(); if (takenPhotoUriString == null) { return null; } Uri takenPhotoUri = Uri.parse(takenPhotoUriString); ContentResolver cr = caller.getContentResolver(); File out = new File(getPhotoFilePath(takenPhotoUri, cr)); if (!out.isFile() || (out.length() == 0)) { cr.delete(takenPhotoUri, null, null); } else { return out; } } catch (Exception ex) { // no-op } return null; } 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; } }