简体   繁体   English

为什么无法从android应用程序的网址加载图片?

[英]Why image is not loading from url of android application?

I have created an android application where image is loading from given url.When I am passing url directly, the image is loading. 我创建了一个Android应用程序,其中图像从给定的URL加载。当我直接传递URL时,图像正在加载。 But image is not loading when I am taking the url inside a string variable and passing that variable . 但是,当我将URL放入字符串变量并传递该变量时,图像无法加载。 My code is given below . 我的代码如下。

private Drawable ImageOperations(String url, String saveFilename) {
        try {
            String realImageUrl=url+"?  email="+Constants.email+"&proc_date="+Constants.proc_date+"&access_key="
            +Constants.ACCESS_KEY+"&version=1.00";

            String newUrl=realImageUrl.replace("https", "http");

            InputStream is = (InputStream) this.fetch(newUrl);



            Log.e("https,SubString http: ",realImageUrl+","+ a);
            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;
    }

This code is not working. 该代码不起作用。 my new url is newUrl. 我的新网址是newUrl。 when I am printing newUrl in my log and giving that url directly instead of newUrl the image is loading. 当我在日志中打印newUrl并直接提供该URL而不是newUrl时,图像正在加载。

I have found the solution. 我找到了解决方案。 I have updated the fetch(String address) method. 我已经更新了fetch(String address)方法。

public Object fetch(String address) throws MalformedURLException,
            IOException {
        try{
        URL url = new URL(address);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
        url = uri.toURL();
        Log.i("Url:", url+"");
        Object content = url.getContent();
        return content;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM