0

So, here is the thing. I am receiving an imagepath through WebService. I am storing the imagepath in a String. Now I want to convert the String to Bitmap and display the image in an imageView. I tried many codes from the examples in the internet but are not working.

Try 1:

      Bitmap bm = BitmapFactory.decodeFile(imagelogo);
      imageView2 = (ImageView) findViewById(R.id.imageView2);
      imageView2.setImageBitmap(bm);

Try 2: First I am converting String to string Base64 and then string Base64 to Bitmap.

     byte[] data;
     String base64;
      {
       try {
          data = imagelogo.getBytes("UTF-8");
          String base64 = Base64.encodeToString(data, Base64.DEFAULT);
          Log.i("Base 64 ", base64);
          } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
          }
      }

      public Bitmap StringToBitMap(String encodedString){
       try {
          byte [] encodeByte=Base64.decode(base64 ,Base64.DEFAULT);
          Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
          return bitmap;
       } catch(Exception e) {
          e.getMessage();
          return null;
       }
    }          

Any help would be appreciated. Thanks in advance.

Koyel
  • 1
  • 1
  • 7

2 Answers2

1

Oh God, you can't convert a String of image path into a Bitmap! The image path isn't the image itself! You need to download the image from internet through the image path and store it into a local file. The you need to call this:

Bitmap bm = BitmapFactory.decodeFile(localImagePath);

to load the Image.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
  • 2
    You don't **need** to store it in a file. – weston Mar 30 '15 at 08:33
  • @weston yes, indeed. I just try to tell the poster the difference between the image path linked to the the server and one linked to the local storage system. – SilentKnight Mar 30 '15 at 08:38
  • 1
    Yeah I get that, but remember they will take what you write literally. And they will look up how to download and store in a file and then where to put that file in android and they will add file system permissions and they simply don't need to go down that route. – weston Mar 30 '15 at 08:53
1

Use this link:

http://www.androidhive.info/2012/07/android-loading-image-from-url-http/

Its very simple and then use this code:

int loader = R.drawable.loader;

    // Imageview to show
    ImageView image = (ImageView) findViewById(R.id.image);

    // Image url
    String image_url = "http://api.androidhive.info/images/sample.jpg";

    // ImageLoader class instance
    ImageLoader imgLoader = new ImageLoader(getApplicationContext());

    // whenever you want to load an image from url
    // call DisplayImage function
    // url - image url to load
    // loader - loader image, will be displayed before getting image
    // image - ImageView
    imgLoader.DisplayImage(image_url, loader, image);

Hope it will helps you..

Avishek Das
  • 661
  • 1
  • 6
  • 20