2

可能重复:
来自 url 的 Android 图像视图

我正在尝试从 url 获取图像并加载imageView,但我无法对 url 字符串进行编码,请告诉我如何编码。

请找到供您参考的代码和网址。

网址:http: //images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG

代码

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;
}
4

5 回答 5

1

使用此代码它将从服务器获取图像

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;
    }
于 2013-01-09T07:27:12.537 回答
1

仅供参考, URLEncoder.encode(String) 已被弃用。

参考:http: //developer.android.com/reference/java/net/URLEncoder.html

于 2013-01-09T07:45:46.267 回答
0

您可以对 url 进行如下编码:

  String URL = "http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG";
  String encodeUrl=URLEncoder.encode(URL, "utf-8");
  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;
}
于 2013-01-09T07:34:39.387 回答
0

请使用以下代码将图像下载并显示到 imageview 中。

public class MainActivity extends Activity {

    ImageView my_img;
    Bitmap mybitmap;
    ProgressDialog pd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView mImgView1 = (ImageView) findViewById(R.id.mImgView1);

        DownloadImageFromURL mDisImgFromUrl = new DownloadImageFromURL(mImgView1);

        mDisImgFromUrl.execute("http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG");

    }

    private class DownloadImageFromURL extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pd = new ProgressDialog(MainActivity.this);
            pd.setMessage("Loading...");
            pd.show();
        }

        public DownloadImageFromURL(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
            pd.dismiss();
        }
    }
}
于 2013-01-09T07:35:17.113 回答
0

我已经为如何从 URL 显示图像创建了示例。使用它适用于我的那个例子,我也通过替换你的图片网址来检查它。

于 2013-01-09T07:43:16.220 回答