get Uri From Camera Result - Android Camera

Android examples for Camera:Camera Attribute

Description

get Uri From Camera Result

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

import android.widget.Toast;

public class Main {
    public static int CAMERA_RESULT = 200;

    public static String getUriFromCameraResult(Activity activity,
            int request, int result, Intent intent) {
        String image_string = null;
        if (request == CAMERA_RESULT) {
            if (result == Activity.RESULT_OK) {
                image_string = getStringUriFromCameraResult(activity);
            } else if (result == Activity.RESULT_CANCELED) {
                Toast.makeText(activity, "Camera has failed",
                        Toast.LENGTH_LONG).show();
            }// w ww  .j a v  a 2 s. c  om
        }
        return image_string;
    }

    private static String getStringUriFromCameraResult(Activity activity) {
        Uri uri = null;
        File file = getFileToStoreCameraResult();
        try {
            uri = Uri.parse(android.provider.MediaStore.Images.Media
                    .insertImage(activity.getContentResolver(),
                            file.getAbsolutePath(), null, null));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (uri != null) {
            return uri.toString();
        }
        return null;
    }

    public static File getFileToStoreCameraResult() {
        String directoryPath = Environment.getExternalStorageDirectory()
                + "/data/com.raulete.evoluzzion/tmp/";
        File directory = new File(directoryPath);
        if (!directory.exists())
            directory.mkdirs();
        String filePath = directoryPath + "camera_capture";
        File file = new File(filePath);
        return file;
    }
}

Related Tutorials