1

In Java server I fetch image from external service URL like:

InputStream in = new java.net.URL(imageWebServiceURL).openStream();
String resultToCleint = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(IOUtils.toByteArray(in));

Then on Android I parse it like:

byte[] imageAsBytes = Base64.decode(resultToCleint.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));

Result: Image not displayed, ain't errors/exceptions neither on server nor on client.

What is the problem here?

EDIT: On android I use class android.util.Base64

Thanks,

michael
  • 3,835
  • 14
  • 53
  • 90
  • let's assume `base64Content` in the base64 string reponsed from web service, you can use `byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);` – BNK Oct 09 '15 at 08:42
  • 1
    Moreover, if your server compressed reponse data either by gzip or deflate, your client side must decompressed the data first – BNK Oct 09 '15 at 08:45
  • @BNK it's not the case the response is regular Json – michael Oct 09 '15 at 08:56
  • I know that, in my project, for example, `String base64Content = jsonObject.getString("Base64Content");`. My web service is Asp.Net Web API, and I used `Convert.ToBase64String(...)` – BNK Oct 09 '15 at 09:00
  • @BNK do you have answer for this question? – michael Oct 09 '15 at 09:30
  • You mean I will add my comments as an answer? – BNK Oct 09 '15 at 09:32
  • 1
    @BNK if you know how to fix this so why not? Only via answer you can gain reputation etc. – michael Oct 09 '15 at 09:35
  • I added, however, in mobile phone, it's difficult to format code :) – BNK Oct 09 '15 at 09:41

3 Answers3

3

Use Picasso library to load image:

You just need to add 1 line of code to show the image on ImageView

//Loading image from below url into imageView

Picasso.with(this)
   .load("YOUR IMAGE URL HERE")
   .into(imageView);

You can learn more from here

Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77
  • The problem that here I don't have the url, rather the image data as decoded base64. so I thought maybe work with url link, and asked this: http://stackoverflow.com/questions/33036066/java-google-places-photos-how-to-get-image-link – michael Oct 10 '15 at 13:38
2

use this to convert to base 64

public static String uploadPic(Bitmap bm) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        String encoded = ""+ Base64.encodeToString(byteArray, Base64.DEFAULT);
        return encoded;
    }


check if image is uploaded then using volley String request object download the string response using this code convert it back.

public Bitmap StringToBitMap(String encodedString){
   try {
      byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
      Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
      return bitmap;
   } catch(Exception e) {
      e.getMessage();
      return null;
   }
}
Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
  • I forgot to mention: the image url is external service and I have my own server (back end) making this calls to externals services in order to later pass them to android... – michael Oct 09 '15 at 08:04
1

As commented, let's assume base64Content is the base64 string responsed from your web service/server-side app, you can refer to the following sample code:

String base64Content = jsonObject.getString("Base64Content");
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 

Moreover, if your server compressed reponse data either by gzip or deflate, your client app must decompress the data first.

Hope this helps!

BNK
  • 23,994
  • 8
  • 77
  • 87
  • this is exactly what I do in Android and it's not working – michael Oct 09 '15 at 09:52
  • Any logcat information available or not? Have you checked your server side? – BNK Oct 09 '15 at 09:54
  • 1
    In your question, you used resultToCleint.getBytes(). Remove getBytes then check again. – BNK Oct 09 '15 at 09:58
  • I think the problem might be on the server side – michael Oct 09 '15 at 10:03
  • Ok, post more code, both client side and server side. One more thing, have you tried to debug your app yet? Post the value of the variables too – BNK Oct 09 '15 at 10:04
  • The service url i'm using to get photos is google places photos search. they say the response is image. maybe here is the problem? necause image can have several formats? when I put the URL in the browser I get blank web page with only image https://developers.google.com/places/web-service/photos – michael Oct 09 '15 at 10:11
  • 1
    So, if you have direct url that response the image, here is the binary data, not the base64 string, you can try loading it with Picasso or by using a volley/okhttp...request – BNK Oct 09 '15 at 10:16
  • but the url request must be on server side and then in some way pass the image via json – michael Oct 09 '15 at 10:17
  • 1
    It depends on your requirement. As I said, if you have a direct link to the image, you can try Picasso, it's good :). I am busy now, I will reply your new comments soon, goodbye! – BNK Oct 09 '15 at 10:21
  • @BKN Maybe this approach will be better (my new question)? http://stackoverflow.com/questions/33036066/java-google-places-photos-how-to-get-image-link – michael Oct 09 '15 at 10:44
  • 1
    I have not ever tried that google service before. Tomorrow if have free time, i'll learn more – BNK Oct 09 '15 at 12:16