简体   繁体   中英

android save bitmap without compression

I'm working on android. I need to save a bitmap without quality lost.

I have tried it using compress() method of Bitmap, setting quality to 100. This not works for me, the bitmap loses too much quality. I have tested with JPEG and PNG formats.

I have tested to copy the bitmap to a ByteBuffer , then extract the byte[] and saving it using a FileOutputStream . In this case, I can't display images saved using this way, I only see a blank page.

My question is, is there any way to save a bitmap in SD card without compression?

EDIT: here part of my code

public class MainActivity extends ActionBarActivity {

    [...]

    @Override
    protected void onActivityResult(int reqCode, int resCode, Intent handler){
        super.onActivityResult(reqCode, resCode, handler);
        if(reqCode == CAPTURE_IDENTIFIER && resCode == Activity.RESULT_OK){
// THis is that I have tested.
//          Object o = handler.getData();
//          Object o2 = handler.getExtras().getParcelable("data");
//          Object o3 = handler.getExtras().get("data");
//          Object o4 = handler.getParcelableExtra("data");
//          
            prepareCapture();
            startActivityForResult(cameraHandler, CAPTURE_IDENTIFIER);
        }
    }



    private void initCamera(){
        cameraHandler = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraHandler.putExtra("return-data", true);
    }

    private void prepareCapture(){
        currentPhotoName = getNextImageIdentifier() + IMG_BASE_NAME;
        File image = new File(capturesFolder, currentPhotoName);
        Uri uriImage = Uri.fromFile(image);
        cameraHandler.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
    }



}

THe exception i have detected is: RuntimeException: failure delivering result ResultInfo [...] NullPointerException

EDIT2: What I have to actually do:

I have to take a photo with the camera, then before save it, I have to process it. After that, i have to save it in SD card. But it's very important the image quality, I can't save it with low quality, or not too low quality at least.

I have been read the google documentation and this is what I understand, but I'm not sure 100%:

The only way to get an uncompressed image from the android camera is saving it in SD card. Then, if you want to use it, get it from SD.

Indeed, it is not possible to pass an uncompressed image between activities by using intents due to intents can only "transport" objects of "small" size.

if you are using IMAGE_CAPTURE, there is a flag that has to be set to not return a thumbnail.

Without seeing your code, I can only guess.

Documentation:

Image Capture Reference

Documentation Quote:

If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field

It's a pretty common mistake, please clarify if it's something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM