如何在Android中将位图转换为jpeg文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20329090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a bitmap to a jpeg file in Android?
提问by Carlo Matulessy
I'm a little bit lost here. I have to convert a bitmap from a cropped image to an .jpeg file. I have looked to other related questions but none of them were relative to mine. (most were reverted as file to bitmap)
我有点迷失在这里。我必须将位图从裁剪图像转换为 .jpeg 文件。我查看了其他相关问题,但没有一个与我的相关。(大多数被恢复为文件到位图)
Thanks in advance
提前致谢
ps. first time Android development
附:第一次安卓开发
回答by Piyush
Use this:
用这个:
Bitmap bmp = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
for that you can use this:
为此,您可以使用它:
FileInputStream fileInputStream = null;
File file = new File("yourfile");
byteArray = new byte[(int) file.length()];
try {
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
//convert array of bytes into file
FileOutputStream fileOuputStream =
new FileOutputStream("C:\testing2.txt");
fileOuputStream.write(bFile);
fileOuputStream.close();
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
and also for more info go with here
以及更多信息去这里
回答by Karthi
Try this
尝试这个
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outStream);
Here is a sample Program
这是一个示例程序
回答by Pooja Sangle
I think this is what you need
我认为这就是你所需要的
bitmap.compress(CompressFormat.JPEG, 90, outputStream);
I hope this will help you.
我希望这能帮到您。