【发布时间】:2013-05-11 12:49:04
【问题描述】:
多年来,我一直在四处寻找,试图让这一切顺利进行,但我无法显示位图。我有一个使用 id 调用的 Web 服务,它返回一个 BLOB 图像,我将其存储在字符串 (img) 中
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http:site.com?GETBLOB&__blobRef=1&USER_ID=6379");
HttpResponse response = httpclient.execute(httpget);
BufferedReader in = new BufferedReader(new
InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null)
{
sb.append(line + NL);
}
in.close();
img = sb.toString();
字符串 img 包含当我将其输出到 textview 时似乎是有效的结果,所以我很确定上面的代码工作正常。问题是将该字符串转换为位图。我尝试了很多方法,但据我所知,这似乎是公认的最佳方法。
byte[] decodedString = Base64.decode(img, Base64.DEFAULT);
bm = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
ImageView imgbox = (ImageView)findViewById(R.id.imagebox);
imgbox.setImageBitmap(bm);
这对我不起作用。对于我尝试过的大多数方法,它只显示任何内容,但对于这种特定方法,它会导致应用程序在加载时崩溃(即使我有一个 try/catch。)
【问题讨论】:
-
检查您的 base64 输入。它可能使用不同的标志(可能是 NO_WRAP)
-
检查你的 base64 字符串,看看它是否被分成 80 个字符的行。还请定义“不工作”,并发布堆栈跟踪。
-
如果应用程序崩溃,将堆栈跟踪添加到您的帖子中会很有用。