Sameer Bidi
Sameer Bidi

Reputation: 3

(Android Studio) How to save bitmap to Internal Storage?

Im trying to save my bitmap file as to png to my Internal Storage How can i do it?

My code to generate QR Code :-

public void createQRCode(String qrCodeData, String charset, Map hintMap, int qrCodeheight, int qrCodewidth) {
    Bitmap bitmap = null;
    try {
        //generating qr code in bitmatrix type
        BitMatrix matrix = new MultiFormatWriter().encode(new String(qrCodeData.getBytes(charset), charset), BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
        //converting bitmatrix to bitmap
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];
        // All are 0, or black, by default
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
            }
        }

        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        qrImageView.setImageBitmap(bitmap);

    } catch (Exception er) {
        Log.e("QrGenerate", er.getMessage());
    }

}

I want to save the generated QR code bitmap to my Internal Storage for example say in DCIM folder i want make a new folder "QR CODES" then i want to save my bitmap as "QRCode1.png"

How can i do this?

i have tried many tutorials and none of them seem to work for me or i am doing it wrong maybe

PS

i have already added the write permission to my AndroidManifest file

Upvotes: 0

Views: 952

Answers (1)

Alok
Alok

Reputation: 9018

Try doing this and then use the data to save it in your local device.

saveImageBitmap(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
    byte[] data = baos.toByteArray();

  //do whatever you want to save this bitmap file 
}

Hope that'll help you out! This will transform the bitmap into .jpg format and then save it in your file.

Upvotes: 1

Related Questions