0

我需要在源代码中传输几个文件。可怕,我知道,但我别无选择。到目前为止,我只存储图像,所以我这样做:

编码图像:

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 以便将新图像放入程序中更容易,因为它只是一个简单的字符串。

现在我正在尝试将功能扩展到任何文件格式,因此我寻求指导以找出在代码中存储文件的方式是否更糟糕。

4

2 回答 2

0

如果你想保留一些数据文件,你可以将它们保存在 Assets 文件夹中,你可以使用 AssetManager 来获取这些文件。

于 2012-10-12T16:27:29.923 回答
0

对于图像,您应该使用资源文件夹。为此,它已内置于系统中。对于一般的最终结果,您应该使用资产文件夹并访问,就像这个 stackoverflow 问题所示:如何从资产文件中获取 URI?

于 2012-10-12T15:50:14.833 回答