-1

I have Bitmaps on my project and I want download my bitmap to gallery from url . How Can I ?

I did use this for create QR code

private Bitmap stringToBitmap(String content){
        try {
            QRCodeWriter writer = new QRCodeWriter();
            BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, 512, 512);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
                }
            }
            return bmp;
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
    }
}

I want download bitmap to my gallery in there

 @Override

    public void onClick(View view) {
        Intent i;
        switch (view.getId()) {
            case R.id.qrindir:
                Bitmap imageResult = stringToBitmap(texttt.getText().toString());
//I want download bitmap in there
                break;

        }
    }

I use this for stringtoBitmap

olustur.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Bitmap imageResult = stringToBitmap(texttt.getText().toString());
       }                });

            }
ogi_plus
  • 75
  • 9

2 Answers2

1

If I get your question correctly, you want to save a bitmap to your internal storage.

In such case you can use Bitmap.compress() method in way like,

try {
   FileOutputStream outstream = new FileOutputStream(path-to-save);
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstream);
   outstream.close();
} catch (IOException e) {
   ...
}

Also make sure in order to write file to internal (primary) storage you need to request WRITE_EXTERNAL_STORAGE at runtime, this may not work for devices above Android 10 (API 29). In such case you can use SAF to create a document.

kaustubhpatange
  • 442
  • 5
  • 13
1

Try this:

private final void saveJpeg(@NonNull Bitmap bitmap, @NonNull String fileName) throws IOException {
    // create a file output stream that will connect to external media store
    OutputStream fos = null;

    ContentResolver cr = getApplicationContext().getContentResolver();
    ContentValues cv = new ContentValues();

    // set meta data for your file
    cv.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
    cv.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
    cv.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);

    // get uri for new image file
    Uri imageUri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);

    // connect file output stream
    fos = cr.openOutputStream(imageUri);

    // compress bitmap and write to output stream
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

    // close file out stream
    fos.flush();
    fos.close();
}

This will compress your bitmap as jpg and write to gallery

Also, remember to get permission in your AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Kyle
  • 154
  • 3
  • 9