for API level <8 to get a top-level public external storage directory where Camera photos should be placed. Empty photo is inserted, path of its parent directory is retrieved and then photo is deleted. If photo cannot be inserted eg. - Android Android OS

Android examples for Android OS:OS Version

Description

for API level <8 to get a top-level public external storage directory where Camera photos should be placed. Empty photo is inserted, path of its parent directory is retrieved and then photo is deleted. If photo cannot be inserted eg.

Demo Code


//package com.java2s;

import android.content.ContentResolver;
import android.content.ContentValues;

import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final String[] PROJECTION = new String[] { MediaStore.MediaColumns.DATA };
    private static final ContentValues CONTENT_VALUES = new ContentValues(1);

    /**//  w  w w. j a va  2s . co  m
     * Dirty hack for API level <8 to get a top-level public external storage directory where Camera photos should be placed.<br>
     * Empty photo is inserted, path of its parent directory is retrieved and then photo is deleted.<br>
     * If photo cannot be inserted eg. external storage is not mounted, then "DCIM" folder in root of the external storage is used as a fallback.
     * @param cr {@link ContentResolver} used to resolve image Uris
     * @return path to directory where camera app places photos (may be fallback)
     */

    @SuppressWarnings("unused")
    public static String getPhotoDirPath(ContentResolver cr) {
        String fallback = Environment.getExternalStorageDirectory()
                + "/DCIM";
        Uri takenPhotoUri;
        String photoFilePath = null;
        try {
            takenPhotoUri = cr.insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    CONTENT_VALUES);
            if (takenPhotoUri == null)
                return fallback;
            photoFilePath = getPhotoFilePath(takenPhotoUri, cr);
            cr.delete(takenPhotoUri, null, null);
        } catch (Exception ex) {
            //igonred
        }
        if (photoFilePath == null)
            return fallback;
        String parent = new File(photoFilePath).getParent();
        Matcher m = Pattern.compile("/DCIM(/|$)").matcher(parent);
        if (m.find())
            parent = parent.substring(0, m.end());

        return parent;
    }

    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;
    }
}

Related Tutorials