Question

I can take a picture via the following intent

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

Then in the onActivityResult method I set the image taken to an ImageView and attempt to store the image to the SD card with the following function

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

When I check the PICTURES folder i see the file but the image is blank and always 12kb in size. Can't I use my above method to save the image from a bitmap file?

Was it helpful?

Solution

Yes as per the CommonsWare comment you can use EXTRA_OUTPUT like this:

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top