如何在Android中将位图转换为jpeg文件? [英] How to convert a bitmap to a jpeg file in Android?
本文介绍了如何在Android中将位图转换为jpeg文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个裁剪的位图图像,我需要将它保存到一个 jpeg 文件中.
提前致谢
解决方案
使用这个:
位图 bmp = null;ByteArrayOutputStream 流 = new ByteArrayOutputStream();bmp.compress(Bitmap.CompressFormat.JPEG, 100, 流);byte[] byteArray = stream.toByteArray();
为此你可以使用这个:
FileInputStream fileInputStream = null;File file = new File("你的文件");byteArray = new byte[(int) file.length()];尝试 {//将文件转换为字节数组fileInputStream = new FileInputStream(file);fileInputStream.read(bFile);fileInputStream.close();//将字节数组转换为文件FileOutputStream 文件输出流 =new FileOutputStream("C:\testing2.txt");文件输出流.write(bFile);文件输出流.close();System.out.println("完成");} 捕获(异常 e){e.printStackTrace();}
以及更多信息,请访问这里>
I have a cropped bitmap image and I need to save it into a jpeg file.
Thanks in advance
解决方案
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
这篇关于如何在Android中将位图转换为jpeg文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文