【问题标题】:unable to fetch image from url in android [duplicate]无法从android中的url获取图像[重复]
【发布时间】:2013-01-09 07:23:22
【问题描述】:

可能重复:
Android image view from url

我正在尝试从 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;
}

【问题讨论】:

  • 请看我编辑的答案,它会解决你的问题。

标签: android


【解决方案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;
    }

【讨论】:

  • OpenHttpConnection是一个方法吗?
  • 是的,我已经添加了这个功能。谢谢,也为我的错误感到抱歉。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    您可以将网址编码如下:

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

    【讨论】:

      【解决方案4】:

      我为How to display image from URL 创建了示例。使用它适用于我的那个例子,我也通过替换你的图片网址来检查它。

      【讨论】:

      • @Pradeep Kumar 你解决了吗?
      【解决方案5】:

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

      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();
              }
          }
      }
      

      【讨论】:

      • 我在异步任务中做,我没有得到图像。
      • @PradeepKumar 查看我编辑的答案。
      猜你喜欢
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 2015-05-23
      • 2016-03-26
      • 1970-01-01
      相关资源
      最近更新 更多