Android将创build的位图保存到SD卡上的目录
我创build了一个位图,现在我想将该位图保存到某个目录。 任何人都可以告诉我如何做到这一点。 谢谢
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卡上的文件夹)
嗨您可以将数据写入字节,然后在SD卡文件夹中创build一个文件,使用任何名称和扩展名,然后将字节写入该文件。 这将把位图保存到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();
你也可以试试这个。
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());
_bitmapScaled.compress()
应该做的伎俩。 查看文档: http : //developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat ,int,java.io.OutputStream)
嘿,把名字给.bmp
做这个:
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); _bitmapScaled.compress(Bitmap.CompressFormat.PNG, 40, bytes); //you can create a new file name "test.BMP" in sdcard folder. File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.bmp")
这听起来是IM只是愚蠢的,但尝试一旦它会得到保存在bmp文件..干杯
这个答案是对OOM和其他各种泄漏的更多考虑的更新。
假设您有一个目标作为目的地和已经定义的名称string。
File destination = new File(directory.getPath() + File.separatorChar + filename); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); source.compress(Bitmap.CompressFormat.PNG, 100, bytes); FileOutputStream fo = null; try { destination.createNewFile(); fo = new FileOutputStream(destination); fo.write(bytes.toByteArray()); } catch (IOException e) { } finally { try { fo.close(); } catch (IOException e) {} }
将位图传递给saveImage方法,它会将您的位图保存在创build的testing文件夹内的saveBitmap名称中。
`
saveImage(bitmap); private void saveImage(Bitmap data) { File createFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test"); if(!createFolder.exists()) createFolder.mkdir(); File saveImage = new File(createFolder,"saveBitmap.jpg"); try { OutputStream outputStream = new FileOutputStream(saveImage); data.compress(Bitmap.CompressFormat.JPEG,100,outputStream); outputStream.flush(); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }