Android - 压缩位图以上传到MySQL数据库

时间:2014-09-09 13:01:31

标签: android bitmap

当我用相机拍照时,它应该被压缩并上传到mysql db。

我检查了函数Bitmap.compress(),输出是在ByteArrayOutputStream中,但我需要File或FileInputStream中的输出。是否可以将ByteArrayOutputStream转换为File?

1 个答案:

答案 0 :(得分:1)

是的,它是:

OutputStream outStream = null;
ByteArrayOutputStream byteOutStream = null;
try {  
    File file = new File(Environment.getExternalStorageDirectory() + "/myimage.png");
    outStream = new FileOutputStream(file);
    byteOutStream = new ByteArrayOutputStream();
    byteOutStream.write(bytes);
    byteOutStream.writeTo(outStream);  
} catch (IOException e) {  
    e.printStackTrace();  
} finally {  
    outStream.close();  
}