如何将Bitmap转换为字符串?

时间:2014-09-30 08:47:28

标签: android eclipse image

我有一个位图数组[photo1,photo2,photo3 .......]我想使用数组中的随机位图。我可以通过将位图转换为字符串来将其用作字符串然后改变最后一个号码?此外,我希望转换的String引用数组中的位图。

4 个答案:

答案 0 :(得分:1)

你不能强制一个字符串到一个字符串,即使你可以,这对你想要做什么毫无意义。如果要从位图数组中获取随机位图,可以执行以下操作:

final Random rnd = new Random();
final int randomIndex = rnd.nextInt(array.length);
final Bitmap randomBitmap = array[randomIndex];

PS:在Java中,您应该使用List而不是数组。

答案 1 :(得分:0)

试试这个:

String resultStr = convertBitmapToString (bmp1);

public String convertBitmapToString(Bitmap bitmap){
     ByteArrayOutputStream baos=new  ByteArrayOutputStream();
     bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
     byte [] b = baos.toByteArray();
     String bString = Base64.encodeToString(b, Base64.DEFAULT);
     return bString;
}

您还可以查看:Convert String to Bitmap and Bitmap to String

希望这有帮助。

答案 2 :(得分:0)

为什么不生成一个随机数来引用数组中的索引?

Random rand = new Random();
int randomIndex = rand.nextInt(bitmaps.length);
Bitmap randomBitmap = bitmaps[randomIndex];

答案 3 :(得分:0)

我怀疑他有一张地图,位图的名称是photo1,photo2 ......

[照片1,照片2,照片3 .......]

他想要一种方法来生成一个随机数,然后从数组中获取位图(map)。

HashMap<String,Object> bitMaps= new HashMap<String,Object>();
...
Random rand = new Random();
int randomIndex = rand.nextInt(bitMaps.size());
Bitmap randomBitmap = bitmaps.get("photo"+randomIndex ) + 1;

很抱歉,如果我完全误解了这个问题。