我需要在源代码中传输几个文件。可怕,我知道,但我别无选择。到目前为止,我只存储图像,所以我这样做:
编码图像:
Bitmap bm = BitmapFactory.decodeFile(imagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String encodedimage = Base64.encodeToString(b, Base64.DEFAULT);
解码图像:
public static Bitmap decodeBitmap(imageString) {
return BitmapFactory.decodeStream(new ByteArrayInputStream(Base64.decode(imageString, Base64.DEFAULT)));
}
我使用 base64 以便将新图像放入程序中更容易,因为它只是一个简单的字符串。
现在我正在尝试将功能扩展到任何文件格式,因此我寻求指导以找出在代码中存储文件的方式是否更糟糕。