使用Android中的Base64对Bitmap进行编码和解码

时间:2014-07-17 07:12:41

标签: java android

我想对位图图像进行编码和解码,并在将位图图像编码为文本文件后保存,但我无法保存。有任何代码可以帮助吗?

public void encodeTobase64(Bitmap myBitmap2) {

    Bitmap immagex=myBitmap2;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    FileInputStream fileInputStream=null;
    File myDir=new File(Environment.getExternalStorageDirectory().getPath() + "/Message Conceal");
    String fname = ".jpg";
    File file = new File(myDir, fname);

    b = new byte[(int) file.length()];

    try {
        //convert file into array of bytes
        fileInputStream = new FileInputStream(file);
        // byte[] bFile;
        fileInputStream.read(b);
        fileInputStream.close();

        //convert array of bytes into file
        FileOutputStream fileOuputStream = 
              new FileOutputStream("C:/Users/User/Desktop/ImageDecryption/DecryptedImageData.txt"); 
        fileOuputStream.write(b);
        fileOuputStream.close();
    } catch(Exception e) {
        e.printStackTrace();
    } 
}
public Bitmap decodeBase64(String input) {
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
}

0 个答案:

没有答案