[英](Android Studio) How to save bitmap to Internal Storage?
林试图将我的位图文件保存为png到我的内部存储中,该怎么办?
我的代码以生成QR代码:-
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());
}
}
我想将生成的QR码位图保存到我的内部存储中,例如说在DCIM文件夹中,我想创建一个新文件夹“ QR CODES”,然后我想将我的位图保存为“ QRCode1.png”
我怎样才能做到这一点?
我尝试了许多教程,但似乎都没有为我工作,或者我做错了也许
PS
我已经向AndroidManifest文件添加了写入权限
尝试执行此操作,然后使用数据将其保存在本地设备中。
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
}
希望对您有所帮助! 这会将位图转换为.jpg格式,然后将其保存在文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.