Android Camera Result Get getUriFromCameraResult(Activity activity, int request, int result, Intent intent)

Here you can find the source of getUriFromCameraResult(Activity activity, int request, int result, Intent intent)

Description

get Uri From Camera Result

Declaration

public static String getUriFromCameraResult(Activity activity,
            int request, int result, Intent intent) 

Method Source 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  w w.  j  av 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/yourName/tmp/";
        File directory = new File(directoryPath);
        if (!directory.exists())
            directory.mkdirs();
        String filePath = directoryPath + "camera_capture";
        File file = new File(filePath);
        return file;
    }
}