Android - 从网上加载图片不再有效

时间:2012-07-23 17:49:12

标签: android url imageview drawable

我有代码,我正在从另一个项目复制,允许你传递一个网址,它将返回一个图片...然后你将它设置为图像视图,它将显示图片......但在某处下线...它不再显示检索到的图片了...你们可以看看这段代码并告诉我是否有什么问题???

点击活动

void NextPic(View v)
{
    picNum++; //int holding what pic number we are on
    Drawable bPic = LoadImageFromWebOperations(picData[]); //picData = String[] with url data in it
    imgView.setImageDrawable(bPic); //Cast earlier in the code
}

获取图片功能

private Drawable LoadImageFromWebOperations(String url)
{
    try
    {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    }
    catch(Exception e)
    {
        //Custom error handler
        uu.SendError(e.getMessage(), "Main.GetPickTask.LoadImageFromWeboperations(String url)");
        return null;
    }
}

LogCat完全没有显示任何内容......没有捕获到错误...并且pic一直空白...我甚至使用ic_launcher图形作为占位符,所以看看它是否是imageview本身...但是显示几秒然后变黑

以下是我正在使用的示例网址 http://app.guycothal.com/Funnys/2012722133515231.jpg

1 个答案:

答案 0 :(得分:0)

    private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
        try {
            InputStream is = (InputStream) fetch(url);
            Drawable d = Drawable.createFromStream(is, "src");
            return d;
        } catch (MalformedURLException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    }
    public Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }