Ali
Ali

Reputation: 15

How to convert Bitmap to image in android Java

I want to convert my bitmap to the image, not in drawable, I have seen some examples out there they are converting bitmap to drawable and but I need media.image(image) and then I am dealing with that image with further logic. Help me out with this problem In short, I need to convert bitmap to the image that's it.

 Bitmap original_with_water_mark= addWatermark(original_image_bitmap,water_mark_bitmap,300);

I need this original_with_water_mark bitmap to convert into the image to store. But I do not know how to convert that bitmap to image

Because in run function see at start i need mImagewhich is the image i have to store

 @Override
        public void run() {

            ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
            byte[] bytes = new byte[buffer.remaining()];
            buffer.get(bytes);
            FileOutputStream output = null;
            try {
                output = new FileOutputStream(mFile);
                output.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                mImage.close();
                if (null != output) {
                    try {
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

Upvotes: 1

Views: 9976

Answers (2)

Venkatesh
Venkatesh

Reputation: 200

yu can use:-

    private void save(){ 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH);
    filename = sdf.format(new Date());

    try {
        String path = getApplicationContext().getFilesDir().getPath();
        OutputStream fOut = null;
        File file = new File(path, "MYFile"//your file name);
        if (!file.exists()) {
            file.mkdirs();
        }

        File file2 = new File(file, filename + ".png");
        fOut = new FileOutputStream(file2);


        //your bitmap
        original_with_water_mark.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    }
}

Upvotes: 1

GSutey
GSutey

Reputation: 316

You can save it in any image format in local storage.

private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
    Log.d(TAG,
            "Error while creating media file, Please ask for storage permission");
    return;
} 
try {
    FileOutputStream fos = new FileOutputStream(pictureFile);
    image.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.close();
} catch (FileNotFoundException e) {
    Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
    Log.d(TAG, "Error accessing file: " + e.getMessage());
}  

}

private  File getOutputMediaFile(){

File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
        + "/Android/data/"
        + getApplicationContext().getPackageName()
        + "/Files"); 


if (! mediaStorageDir.exists()){
    if (! mediaStorageDir.mkdirs()){
        return null;
    }
}  
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
    String mImageName="MI_"+ timeStamp +".jpg";
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);  
return mediaFile;

}

Upvotes: 0

Related Questions