在尝试使用Android编程中的电子邮件附加生成的QR码作为附件时,我遇到了一些问题。这是我生成QR码然后调用发送电子邮件的部分:
@SuppressLint("NewApi")
private void generateQR() {
// 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;
// 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.ivImage);
myImage.setImageBitmap(bitmap);
new SendEmailAsyncTask().execute(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
class SendEmailAsyncTask extends AsyncTask <Bitmap, Void, Boolean> {
@Override
protected Boolean doInBackground(Bitmap... params) {
GMailSender sender = new GMailSender("something@gmail.com","pwd");
try {
sender.sendMail("Successful Event Registration QR Code",params[0], "something@gmail.com", "something@gmail.com");
System.out.println("send");
} catch (Exception e) {
System.err.println("err"+e);
Log.e("SendMail", e.getMessage(), e);
}
return null;
}
}
但是,我在sendMail上收到错误消息:
The method sendMail(String, String, String, String) in the type GMailSender is not applicable for the arguments (String, Bitmap, String, String)
因为之前当我替换params [0]时,它之前是一个字符串。但是我必须将生成的QR码位图传递到电子邮件内容中。有什么想法吗?
提前致谢。
更新
Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
ImageView myImage = (ImageView) findViewById(R.id.ivImage);
myImage.setImageBitmap(bitmap);
counterQR++;
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "eNeighbourhood"+counterQR+".jpg");
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = getImageBitmap(bitmap); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
使用上面的代码,我试图将位图转换为文件,以便我也可以通过获取file参数来修改sendMail()。但是,我发现getAmageBitmap未定义为MainActivity