3

Ok, I have read all questions and answers about this topic. I have been reading them for a few days and nothing worked for me so I have one straight question. This code generates one String

public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    input = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return input;
}

And this code generate other String from the same PICTURE,

  <?php

$data = file_get_contents('Lake_mapourika_NZ.jpeg');
$nova = base64_encode($data);

    echo $nova;

?>

When I insert them into this code back in Java :

  public void decodeImage()
{
    byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
    bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
    imageView.setImageBitmap(bitmap);
}

First String wont work second works fine. Why ? Why java to java wont work and PHP to java works fine ?

Of course Strings are different that is also good question. Why are they different when they are generated from same bitmap ? To me it seems that Java decode works fine but encode wont work.

I'm using this code to send my pictures from app to server so I cant use online converters every time it has to be encoded to string from app. I don't get any errors and for pictures taken with my phone camera I get out of memory exception. But when change quality to 50 then I get decoded string but it wont work as usual.

Is there any other way to do it ?

abbath
  • 2,444
  • 4
  • 28
  • 40
cfhpanteracfh
  • 115
  • 3
  • 9
  • 1
    Can I ask you why you are using base64 here anyway? – e4c5 Oct 19 '15 at 09:15
  • Instead of getting image's base64 string and covert them for displaying, i suggest you to store the image's url as reference, it makes thing easier to display image directly on a imageview from the image's url. you can refer here http://stackoverflow.com/questions/348363/what-is-the-best-practice-for-storing-uploaded-images – cw fei Oct 19 '15 at 09:25
  • `input` should be a local variable, `String input = ...`. That does not explain the error, so save the bytes in a file, to see whether `compress` worked. Maybe after `?>` there follows a newline (normally wrong), then java would need an explicit newline too `"\r\n"`. – Joop Eggen Oct 19 '15 at 10:02
  • String from PHP works fine in java. Java encode is one that doesn't work either in Java decode or PHP decode. But PHP encode works fine in both places. Yes it needs to be local I forgot to put it back. @JoopEggen – cfhpanteracfh Oct 19 '15 at 10:19
  • I'm trying to sync with wordpress site that I'm creating so when user uploads pictures from app (phone tablet) it need to be uploaded to images folder of wordpress site. @e4c5 – cfhpanteracfh Oct 19 '15 at 10:22
  • How do you think url ? When that picture is on my phone? @cwfei – cfhpanteracfh Oct 19 '15 at 10:22
  • So why do you need to base64 an image to upload it? – e4c5 Oct 19 '15 at 11:06
  • Because I don't know other way. And this is the only example that I have found online. I parse it into string and put it in JSON and POST it to PHP script. Then I read the data. Is there any other way ? And please if you know tell me I've been struggling with this almost three days. @e4c5 – cfhpanteracfh Oct 19 '15 at 11:25

2 Answers2

2

There is error in your code, but not the code you have posted. Check the content of bitmap you encode, and check that you are using correct input string variable while decoding.

Your encoding, decoding part works fine. That code would be better if you would not rely on member fields, though.

public String getStringImage(Bitmap bmp)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String input = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return input;
}

public void decodeImage(ImageView iv, String input)
{
    byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
    iv.setImageBitmap(bitmap);
}

Test code for above encoding/decoding methods - modified to use parameters instead of member fields.

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.thn);
ImageView iv = (ImageView)findViewById(R.id.image_view1);
iv.setImageBitmap(bm);

iv = (ImageView)findViewById(R.id.image_view2);
String b = getStringImage(bm);
decodeImage(iv, b);

Answer to why you are getting different base64 string from Java encoding and PHP encoding is rather simple. With PHP you are taking image file and encode it as-is, and with Java you are maybe starting with same image file, but then you are first decoding it to bitmap and then you are encoding that bitmap to JPEG stream (that will produce different JPEG image from the one you have started with) that you finally encode to base 64.

PHP: JPEG file -> base64

Java: JPEG file -> Bitmap -> JPEG stream (different from JPEG file) -> base 64
Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
  • Thank you. Your answer stopped me from jumping through window. I have lost hours and hours trying to understand this. Thank you. @Dalija Prasnikar – cfhpanteracfh Oct 19 '15 at 12:31
  • Ok so I managed to upload file to server I have wrote http entity part in java and receiving part in PHP everything works fine but I keep getting files that are very small so I've tried to see the content of my file [link](http://stackoverflow.com/questions/33275893/conversion-from-file-to-bitmap-in-android) Can you help me ? I'm sorry if I'm bothering you but I really need help. @Dalija Prasnikar – cfhpanteracfh Oct 22 '15 at 13:01
1

This is not a direct answer to the question but, I think still the right answer! Base64 encoding/decoding is absolutely not needed for this task.

Images and other files are uploaded to web servers using the multipart/form-data encoding

The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

The good news is that you don't need to do the encoding yourself. All popular http client libraries handle it internally. You just need to tell them what files to upload. Here is an answer showing how to do it with Volley. Here is an answer that shows how to do it with loopj async http client.

It seems that you are using PHP at the server side. As you very well know, PHP has excellent built in support for accepting file uploads.

Base64 encoding/decoding is not needed at either end. Please also note that when you base64 encode a binary file it's size increases by about 30% so your base64 encoded post will increase memory, cpu and data usage on the device.

Community
  • 1
  • 1
e4c5
  • 52,766
  • 11
  • 101
  • 134
  • Ok. I think I see a solution, it's just hard for me to wrap my mind around it when I see this much new stuff. I would like to up vote your answer but unfortunately I still a newbie to stack. Thank you so much. I will respond If I succeed. @e4c5 – cfhpanteracfh Oct 19 '15 at 12:28
  • Ok so I managed to upload file to server I have wrote http entity part in java and receiving part in PHP everything works fine but I keep getting files that are very small so I've tried to see the content of my file [link](http://stackoverflow.com/questions/33275893/conversion-from-file-to-bitmap-in-android) Can you help me and is this against the rules of this site ? To seek help like this ? If it is I'm sorry I'm not going to bother anyone anymore. :( @e4c5 – cfhpanteracfh Oct 22 '15 at 12:55
  • 1
    First glance at your code in that Q shows that you are doing double conversion (file to bitmap and back to file data again) but you can upload directly knowing just the file name (refer the volley example linked in my answer) – e4c5 Oct 22 '15 at 13:46
  • I did that because I got files on server that are really small. But with right name and type. So I thought that maybe conversion from bitmap to file isn't good. So I wanted to be sure that there lays the error but it didn't. @e4c5 – cfhpanteracfh Oct 22 '15 at 14:37
  • I have updated my question. Thank you on your response. :) @e4c5 – cfhpanteracfh Oct 22 '15 at 14:56