【发布时间】:2015-03-30 08:22:12
【问题描述】:
所以,事情就是这样。我正在通过 WebService 接收图像路径。我将图像路径存储在字符串中。现在我想将字符串转换为位图并在 imageView 中显示图像。我尝试了互联网上示例中的许多代码,但都无法正常工作。
尝试1:
Bitmap bm = BitmapFactory.decodeFile(imagelogo);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView2.setImageBitmap(bm);
尝试 2:首先我将字符串转换为字符串 Base64,然后将字符串 Base64 转换为位图。
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;
}
}
任何帮助将不胜感激。提前致谢。
【问题讨论】:
-
如果你有图片的 url 并且想在
ImageView中显示它,那么只需使用毕加索库square.github.io/picasso -
好的。谢谢你,M-WaJeEh。这是一个简单而好主意。
-
你为什么使用
imagelogo.getBytes("UTF-8");?您的字符串可能不是 UTF-8 编码的。 -
您遇到的错误或异常是什么?
标签: android string bitmap imageview