Android Saving创建位图到SD卡上的目录

use*_*883 30 android image save

我创建了一个位图,现在我想将该位图保存到某个目录.任何人都可以告诉我这是如何完成的.谢谢

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);
Run Code Online (Sandbox Code Playgroud)

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

kru*_*hah 103

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

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();
Run Code Online (Sandbox Code Playgroud)


And*_*lva 11

你也可以试试这个.

  OutputStream fOut = null;
                        File file = new File(strDirectoy,imgname);
                            fOut = new FileOutputStream(file);

                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                            fOut.flush();
                            fOut.close();

                MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
Run Code Online (Sandbox Code Playgroud)

  • 我更喜欢这个,因为你不必弄乱字节数组 (2认同)