BitmapFactory:无法解码Stream

时间:2013-05-22 15:50:48

标签: android listview android-asynctask bitmapfactory

我是Android开发人员的新手,整天都在研究ListAdapter的问题,但找不到任何帮助我解决问题的答案。

我有一个从URL解析的JSON NewsString,需要将Thumbnail加载到ListView中。 我尝试通过Imageloader加载它(https://github.com/novoda/ImageLoader#directloader-utility),然后将Bitmap转换为Drawable,这样我就可以在ListView中使用它了(之前做过)。

我在Manifest中拥有权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

在AsyncTask中解析JsonObject,我尝试从Url加载图像。

try {
    String url = e.getString("imageThumb");
    Bitmap bmp = new DirectLoader().download(url);
    Drawable d = new BitmapDrawable(null,bmp);
    Log.d("getBitmap", d.toString());
    row.put("thumb", d);
}catch (Exception e5) {
    Log.e("IMG", "Failed to load Thumbnail From URL: "+e5.toString());
    Drawable d = MainActivity.hweb_icon;
    row.put("thumb", d);
}

但我总是得到相同的错误消息: enter image description here

我尝试了几个教程和其他东西,但它从来没有为我做过。 网址是正确的,不需要任何编码。

我以前的方法看起来像这样:

try {

    String url = e.getString("imageThumb");
    InputStream in = new java.net.URL(url).openStream();
    Bitmap bmp = BitmapFactory.decodeStream(in);                                                                                
    Drawable d = new BitmapDrawable(null,bmp);
    row.put("thumb", d);
}catch (Exception e5) {
    // handle it
    Log.e("IMG", "Failed to load Thumbnail From URL : "+e5.toString());

}

很抱歉,如果这个问题已经得到解答,但我找到了很多解决方案并尝试过,但没有一个能帮助我。

THX

pwittke

修改 我创建了一个新的Testproject并且下面的代码运行正常,所以我开始停用每个使用drawables的部分并且结果是,SimpleAdapter无法以这种方式加载图像...它只能从resurces加载图像而不是从url ... thx为你的脚本britzl,它工作正常,现在我需要找到一种方法来更改我的适配器的图像......

1 个答案:

答案 0 :(得分:0)

有很多方法可以从URL加载位图。如果你想自己动手,你可以试试:

public Bitmap loadImage(String url) {
    DefaultHttpClient client = new DefaultHttpClient();
    Bitmap bitmap = null;
    try {
        HttpResponse response = client.execute(new HttpGet(url));
        HttpEntity entity = response.getEntity();
        if(entity != null) {
            InputStream in = entity.getContent();
            bitmap = BitmapFactory.decodeStream(in);
        }
    }
    catch (Exception e) {
    }
    return bitmap;
}

有一些事情需要考虑,例如,如果您遇到Skia解码器错误,您可能需要包装流。 Read more here.

但我建议您查看现有的库。来自Square的即将到来的Picasso库可能是您最好的选择。例如:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
相关问题