在Android中将字符串转换为图像?

Convert String to Image in Android?(在Android中将字符串转换为图像?)

本文介绍了在Android中将字符串转换为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,事情就是这样.我正在通过 WebService 接收图像路径.我将图像路径存储在字符串中.现在我想将字符串转换为位图并在 imageView 中显示图像.我尝试了互联网上示例中的许多代码,但都无法正常工作.

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.

尝试1:

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

尝试 2:首先我将字符串转换为字符串 Base64,然后将字符串 Base64 转换为位图.

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.

推荐答案

使用此链接:

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

希望对你有所帮助..

这篇关于在Android中将字符串转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在Android中将字符串转换为图像?

基础教程推荐