Convert String to Image in 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中将字符串转换为图像?


- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01