gpt4 book ai didi

java - (Android Studio) 如何将位图保存到内部存储?

转载 作者:行者123 更新时间:2023-12-02 12:14:32 25 4
gpt4 key购买 nike

我正在尝试将我的位图文件保存到我的内部存储中我该怎么做?

我的生成二维码的代码:-

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”

我该怎么做?

我尝试了很多教程,但似乎没有一个对我有用,或者我可能做错了

附注

我已经向我的 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 格式,然后将其保存在您的文件中。

关于java - (Android Studio) 如何将位图保存到内部存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46284695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com