0

我有一个 android 应用程序可以将 mysql 数据库中的多个图像加载到 ImageButton 上。

imageButton.setImageBitmap(fetchBitmap("http://www...~.jpg"));

我曾经能够成功加载 png,但现在也失败了(jpg 图像从未成功)。这是我用于下载图像的代码:-

public static Bitmap fetchBitmap(String urlstr) {
    InputStream is= null;
    Bitmap bm= null;
    try{
        HttpGet httpRequest = new HttpGet(urlstr);
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

        HttpEntity entity = response.getEntity();
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
        is = bufHttpEntity.getContent();
        BitmapFactory.Options factoryOptions = new BitmapFactory.Options();
        bm = BitmapFactory.decodeStream(is);
    }catch ( MalformedURLException e ){
        Log.d( "RemoteImageHandler", "Invalid URL: " + urlstr );
    }catch ( IOException e ){
        Log.d( "RemoteImageHandler", "IO exception: " + e );
    }finally{
        if(is!=null)try{
            is.close();
        }catch(IOException e){}
    }
    return bm;
} 

我收到此错误:-

D/skia(4965): --- SkImageDecoder::Factory returned null

我已经尝试过这里建议的各种组合这里其他几个解决方案,但它对我不起作用。我错过了什么吗?图片肯定出现在我输入的网址中。

谢谢你。

4

2 回答 2

0

使用以下代码下载图像并存储到位图中,它可能会对您有所帮助。

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}
于 2012-07-27T06:00:44.017 回答
0

问题是无法下载图像,因为保存图像的目录没有“执行”权限。添加权限后,该应用程序运行顺利:)

于 2012-07-29T13:30:03.550 回答