安卓(Android) 怎么从相机(Camera)结果获取uri

2019年10月27日20:56:00安卓(Android) 怎么从相机(Camera)结果获取uri已关闭评论 352 1341字阅读4分28秒

1.简介

安卓(Android) 怎么从相机(Camera)结果获取uri?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


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

 
继续阅读
编程十万个怎么办