gpt4 book ai didi

Android将创建的位图保存到SD卡上的目录

转载 作者:IT老高 更新时间:2023-10-28 21:49:44 27 4
gpt4 key购买 nike

我创建了一个位图,现在我想将该位图保存到某个目录中。谁能告诉我这是怎么做的。谢谢

FileInputStream in;
BufferedInputStream buf;
try {
in = new FileInputStream("/mnt/sdcard/dcim/Camera/2010-11-16_18-57-18_989.jpg");
buf = new BufferedInputStream(in);
Bitmap _bitmapPreScale = BitmapFactory.decodeStream(buf);
int oldWidth = _bitmapPreScale.getWidth();
int oldHeight = _bitmapPreScale.getHeight();
int newWidth = 2592;
int newHeight = 1936;

float scaleWidth = ((float) newWidth) / oldWidth;
float scaleHeight = ((float) newHeight) / oldHeight;

Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
Bitmap _bitmapScaled = Bitmap.createBitmap(_bitmapPreScale, 0, 0, oldWidth, oldHeight, matrix, true);

(我想将 _bitmapScaled 保存到我 SD 卡上的文件夹中)

最佳答案

您好,您可以将数据写入字节,然后在 sdcard 文件夹中使用您想要的任何名称和扩展名创建一个文件,然后将字节写入该文件。这会将位图保存到 sdcard。

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());

// remember close de FileOutput
fo.close();

关于Android将创建的位图保存到SD卡上的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4263375/

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