I have an array of bitmaps [photo1,photo2,photo3.......] and I want to use a random bitmap from the array.Can I use it as a String by casting a bitmap to the string and then changing the last number? Also I want that the casted String refers to the bitmap from the array.
Asked
Active
Viewed 172 times
-1
-
Try this - http://stackoverflow.com/questions/13562429/how-many-ways-to-convert-bitmap-to-string-and-vice-versa – Aditya Gupta Sep 30 '14 at 08:50
4 Answers
1
You can't cast a Bitmap to a String, and even if you could, that makes no sense for what you want to do. If you want to get a random Bitmap from an array of Bitmaps, you can do as follows:
final Random rnd = new Random();
final int randomIndex = rnd.nextInt(array.length);
final Bitmap randomBitmap = array[randomIndex];
PS: in Java you should really be using List
s instead of arrays.

m0skit0
- 25,268
- 11
- 79
- 127
0
Try this:
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;
}
You can also have a look at: Convert String to Bitmap and Bitmap to String
Hope this helps.

MysticMagicϡ
- 28,593
- 16
- 73
- 124
-
-
That's not what he/she is asking, read the question again ;) *"I have an array of bitmaps and I want to use a random bitmap from the array"* – m0skit0 Sep 30 '14 at 08:52
-
-
@m0skit0 He has written after that "Can I use it as a String by casting a bitmap to the string". so I answered for that :) – MysticMagicϡ Sep 30 '14 at 08:55
-
-
-
If you read the question, you would know what he's asking for: *"I want to use a random bitmap from the array"* It's the first sentence ;) – m0skit0 Sep 30 '14 at 08:57
-
@m0skit0 There is no point in fighting over it. "I want to use a random bitmap from the array" is a statement. "Can I use it as a String by casting a bitmap to the string" seems like a question. :) That's my POV – MysticMagicϡ Sep 30 '14 at 08:58
-
Fight? Who's fighting? :) With *"Can I use it as a String by casting a bitmap to the string"* he/she's asking if that is a valid solution for what he/she wants, and obviously it is not. – m0skit0 Sep 30 '14 at 09:00
-
@m0skit0 may be, its not. I am still at learning stage, so I can't know high level impacts. But I just gave an answer as per my interpretation of question. Have a good day ahead :) take care.. – MysticMagicϡ Sep 30 '14 at 09:01
0
Why not generate a random number to refer to an index in the array?
Random rand = new Random();
int randomIndex = rand.nextInt(bitmaps.length);
Bitmap randomBitmap = bitmaps[randomIndex];

AndroidNoob
- 2,771
- 2
- 40
- 53
0
I suspect he has a map and the name of the bitmap is photo1, photo2 ...
[photo1,photo2,photo3.......]
He want a method to generate one random number and then get the bitmap from the array(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;
Sorry if I totally misunderstand this problem.

paco alcacer
- 381
- 2
- 13