how to save the generated qr code into gallery android

1,203 views
Skip to first unread message

Cyprian Kabia

unread,
Oct 14, 2015, 2:24:02 AM10/14/15
to zxing
hello, first id like to commend the zxing team for the great work they continually do.
i was able to create a qr code generator withing my android app and it works flawlessly and that's why am very impressed with the zxing team. So on to my question, how can i save the generated qr code to the phone's gallery? Below is my sample code that is not working.
public void onClick(View v) {

switch (v.getId()) {
case R.id.button1:
EditText qrInput = (EditText) findViewById(R.id.qrInput);
String qrInputText = qrInput.getText().toString();
Log.v(LOG_TAG, qrInputText);

//Find screen size
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
int width = point.x;
int height = point.y;
int smallerDimension = width < height ? width : height;
smallerDimension = smallerDimension * 3/4;

//Encode with a QR Code image
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(qrInputText,
null,
Contents.Type.TEXT,
BarcodeFormat.QR_CODE.toString(),
smallerDimension);
try {
Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage.setImageBitmap(bitmap);

} catch (WriterException e) {
e.printStackTrace();
}


break;
case R.id.save:
Bitmap bitmap;
OutputStream output;


// Retrieve the image from the res folder

bitmap = BitmapFactory.decodeResource(getResources(),
R.id.imageView1);
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage.setImageBitmap(bitmap);

// Find the SD Card path
File filepath = Environment.getExternalStorageDirectory();

// Create a new folder in SD Card
File dir = new File(filepath.getAbsolutePath()
+ "/Mobilelife Qr Code/");
dir.mkdirs();

// Create a name for the saved image
File file = new File(dir, "myImage.png");

// Show a toast message on successful save
Toast.makeText(GenerateQRCodeActivity.this, "Image Saved to SD Card",
Toast.LENGTH_SHORT).show();
try {

output = new FileOutputStream(file);

// Compress into png format image from 0% - 100%
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
}

catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}


}

Lachezar Dobrev

unread,
Oct 14, 2015, 3:59:04 AM10/14/15
to Cyprian Kabia, zxing


--
You received this message because you are subscribed to the Google Groups "zxing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zxing+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages