Android 将创建的位图保存到 SD 卡上的目录 [英] Android Saving created bitmap to directory on sd card

查看:38
本文介绍了Android 将创建的位图保存到 SD 卡上的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个位图,现在我想将该位图保存到某个目录中.谁能告诉我这是如何完成的.谢谢

I created a bitmap and now i want to save that bitmap to a directory somewhere. Can anyone show me how this is done. Thanks

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 卡上的一个文件夹中)

(I want to save _bitmapScaled to a folder on my SD card)

推荐答案

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

Hi You can write data to bytes and then create a file in sdcard folder with whatever name and extension you want and then write the bytes to that file. This will save bitmap to 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 卡上的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆