无需调整大小即可保存相机中的图像

时间:2017-09-28 18:55:25

标签: android android-camera android-camera-intent

我使用以下代码创建图像文件并将其保存到SD卡

 private File createImageFile(Bitmap bitmap) throws IOException {
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                PICNAME,
                ".png",
                storageDir);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(image);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

问题在于,尽管我以全屏模式拍摄照片,但上述代码始终以非常小的尺寸(320x240)保存图像。为什么这样..有什么办法可以保存图像而不需要调整大小?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

public static Uri takePhotoByCamera(Activity activity) {
    File publicDirectory = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/myFolder");
    double d = new Random().nextDouble();
    File file = new File(publicDirectory, d + ".jpg");
    String path = file.getAbsolutePath();
    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, path);
    values.put(MediaStore.MediaColumns.TITLE, "New Picture");
    values.put(MediaStore.Images.ImageColumns.DESCRIPTION, "From your Camera");
    activity.getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Uri photoUri = Uri.parse("file://" + path);
    if (!publicDirectory.exists())
        publicDirectory.mkdirs();
    else if (!publicDirectory.isDirectory() && publicDirectory.canWrite()) {
        publicDirectory.delete();
        publicDirectory.mkdirs();
    } else {
        Log.d("tag550", "can't access");
    }
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    activity.startActivityForResult(intent, requestCamera);
    return photoUri;
}