0

我可以通过以下意图拍照

cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

然后在onActivityResult方法中,我将拍摄的图像设置为 anImageView并尝试使用以下功能将图像存储到 SD 卡

private void savePic(Bitmap bmp) {
    if(!isSDOK || !isSDWritable)
        return;



        String name = "TESTA";
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File file = new File(path,name+".jpg");


        path.mkdirs();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
        bmp.compress(CompressFormat.JPEG, 100 /*ignored for PNG*/, bos); 
        byte[] bitmapdata = bos.toByteArray();
        ByteArrayInputStream bis = new ByteArrayInputStream(bitmapdata);

        try{
            InputStream is = bis;
            OutputStream os = new FileOutputStream(file);
            byte[] data = new byte[is.available()];
            is.read();
            os.write(data);
            is.close();
            os.close();
        }catch(Exception e){Log.e("IMAGE CONVERT ERR", e.toString());}
        return;
}

当我检查PICTURES文件夹时,我看到了文件,但图像是空白的,并且大小始终为 12kb。我不能使用上述方法从位图文件中保存图像吗?

4

1 回答 1

1

是的,根据 CommonsWare 评论,您可以像这样使用 EXTRA_OUTPUT:

private void capturePhoto() {
    File root = new File(Environment.getExternalStorageDirectory(), "Folder");
    if (!root.exists()) {
        root.mkdirs();
    }
    File file = new File(root, "filename.jpeg");
    Uri outputFileUri = Uri.fromFile(file);

    Intent photoPickerIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    photoPickerIntent.putExtra("return-data", true);
    startActivityForResult(photoPickerIntent, TAKE_PICTURE);
}
于 2013-04-21T16:36:46.517 回答