【问题标题】:Storing image taken from camera存储从相机拍摄的图像
【发布时间】:2013-04-21 15:51:55
【问题描述】:

我可以通过以下意图拍照

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

然后在onActivityResult 方法中,我将拍摄的图像设置为ImageView,并尝试使用以下函数将图像存储到 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。我不能用我上面的方法从位图文件中保存图像吗?

【问题讨论】:

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


【解决方案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);
}

【讨论】:

  • 所以没有MediaStore.EXTRA_OUTPUT 文件没有被存储?我明白了,我有一个视图用于保存您使用相机拍摄的一些照片,以便您查看每张照片并选择将哪一张用作主要照片。
猜你喜欢
  • 2011-07-15
  • 2015-10-19
  • 2018-03-14
  • 2017-09-20
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多