nana4ever 2013-01-09 07:53 采纳率: 95.8%
浏览 6112
已采纳

Android中如何编码url字符串?

我想从URL中获取图片,再加载到imageView中。但是我不知道如何编码url字符串。请问如何编码url字符串?
我贴出URL和代码
URL: http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG

Code:

String link=URLEncoder.encode(urlStr);                    
bitmap=loadBitmap(link);

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    try {
        InputStream in = new java.net.URL(url).openStream();
        bitmap = BitmapFactory.decodeStream(in);
    } catch (Exception e) {

    }        
    return bitmap;
}
  • 写回答

3条回答 默认 最新

  • O_Casper_ne 2013-01-09 09:38
    关注

    使用下面的代码可以从服务器中获取图像

    String URL = "http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG"; 
    
    private InputStream OpenHttpConnection(String urlString) throws IOException {
            InputStream in = null;
            int response = -1;
            URL url = new URL(urlString);
            URLConnection conn = url.openConnection();
    
            if (!(conn instanceof HttpURLConnection))
                throw new IOException("Not an HTTP connection");
    
            try {
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect();
    
                response = httpConn.getResponseCode();
                if (response == HttpURLConnection.HTTP_OK) {
                    in = httpConn.getInputStream();
                }
            } catch (Exception ex) {
                throw new IOException("Error connecting");
            }
            return in;
        }
    
    private Bitmap DownloadImage(String URL) {
            Bitmap bitmap = null;
            InputStream in = null;
            try {
                in = OpenHttpConnection(URL);
                bitmap = BitmapFactory.decodeStream(in);
    
                in.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                final String msg = e1.getMessage();
                handler.post(new Runnable() {  
                    public void run() {  
                        Toast.makeText(getApplicationContext(), "" + msg, Toast.LENGTH_SHORT).show();  
                    }  
                });
            }
            return bitmap;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?