Koyel
Koyel

Reputation: 1

Convert String to Image in Android?

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.

Upvotes: 0

Views: 4573

Answers (2)

Avishek Das
Avishek Das

Reputation: 699

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

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);

Upvotes: 1

SilentKnight
SilentKnight

Reputation: 14021

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.

Upvotes: 1

Related Questions