0

I'm uploading a String bitmap as "User profile image". The upload to the server using php is all good, and the download from the server too. The thing is when I'm looking in the bitmap String I see little differences between the 2 bitmaps and I can decode the one I have downloaded. IDK if i'm managing the String in the correct way.

String bitmap I'm sending : 1

String bitmap I receive (pic in comments, i can't put more than 1 link here) [2]

code to receive my string from the php:

    StringBuilder sb = new StringBuilder();
    reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) { 
        sb.append(line + "");
     }
        line = sb.toString();
String[] kvPairs = line.split(",");

Once I split the line by the commas: (im receiving smth like"name":"john","age"="5",...,"bitstringImag"="/9j/4AA.." )

Im getting the Bitmap String value:

String[] bitmstrIn = kvPairs[5].split(":"); //separating the key from the value
            String[] bitmstrIn2 = bitmstrIn[1].split("\\}");  //erasing last key in the String
            String bitStr = bitmstrIn2[0].replaceAll("\"", ""); //removing the added (i dont know why) backslash.
            String biStrFin = bitStr.replaceAll("\\\\","");//removing the added (i dont know why) backslash.

and the result in bitStrFin is the one I pasted in to the comments.

If you know any better way to do it, please tell me i've been fighting to this for a long time! Thanks for the help guys

Álvaro Koke
  • 113
  • 1
  • 10

1 Answers1

0

Use a Base64 String. Link

In my Case it was the best Solution and works very well. I converted the Images via php and show it in our App.

Java: decode

public static Bitmap getBitmapByString(String encodedString){
    byte[] imageBytes = Base64.decode(encodedString, Base64.NO_WRAP);
    return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
}

Java: encode

public static String getStringByBitmap(Bitmap bitmap){
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bitmap.getByteCount());
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    byte[] imageBytes = outputStream.toByteArray();
    try {
        outputStream.close();
    } catch(IOException e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(imageBytes, Base64.NO_WRAP);
}

PHP:

$base64 = base64_encode($imagedata);
$imagedata = base64_decode($base64);

Sorry for the very late answer. Thats the Java function in my Project and the PHP function i used to create test data.

Community
  • 1
  • 1
Manu
  • 182
  • 2
  • 7
  • the problem is that i'm not receiving the same I sent. **Base64 String i'm sending:** /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA **Base64 String i'm receiving:** "image":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\n ... Do you know what can be wrong? – Álvaro Koke Feb 19 '16 at 18:34