如何在Android中将位图转换为jpeg文件?

Car*_*ssy 16 android jpeg file bitmap

我在这里有点失落.我必须将位图从裁剪图像转换为.jpeg文件.我已经查看了其他相关问题,但没有一个与我相关.(大多数都被恢复为文件到位图)

提前致谢

PS.第一次Android开发

Piy*_*ush 30

用这个:

Bitmap bmp = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
Run Code Online (Sandbox Code Playgroud)

为此你可以使用这个:

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

而且有关更多信息,请点击此处


Kar*_*thi 7

尝试这个

bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outStream);
Run Code Online (Sandbox Code Playgroud)

这是一个示例程序

将位图压缩为 jpg 格式 Android