【问题标题】:Android : Load image from web URL [duplicate]Android:从网页 URL 加载图像 [重复]
【发布时间】:2012-08-23 17:20:43
【问题描述】:

可能重复:
Loading remote images

我正在开发一个应用程序,其中有一个列表,其中每一行都有一个图像,

此图像是从 Web URL 加载的。

下面是我的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
            android:id="@+id/list_row_img"
            android:layout_width="200dp"
            android:layout_height="200dp"
            />

    <TextView
            android:id="@+id/list_row_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

</LinearLayout>

这是活动代码

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

    ImageView img= (ImageView) findViewById(R.id.list_row_img);
    Bitmap bm = null;
    try {
         URL aURL = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
         URLConnection conn = aURL.openConnection();
         conn.connect();
         InputStream is = conn.getInputStream();
         BufferedInputStream bis = new BufferedInputStream(is);
         bm = BitmapFactory.decodeStream(bis);
         img.setImageBitmap(bm);
         bis.close();
         is.close();

         } catch (Exception e) {
            Log.v("EXCEPTION", "Error getting bitmap", e);
         }
}

当我跑步时,我在设备上什么也得不到。只有灰屏,没有异常发生。

请注意,我在清单文件中添加了此权限

<uses-permission android:name="android.permission.INTERNET" />

谁能帮帮我?

【问题讨论】:

    标签: android url imageview android-imageview


    【解决方案1】:

    试试这个 你可以通过

    来执行它
          new DownloadImageTask((ImageView)  im1).execute(url here);
    

        private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
    
        public DownloadImageTask(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;
        }
    
    
    /*    @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            showProgressDialog();
        }*/
    
        protected void onPostExecute(Bitmap result) {
            //pDlg.dismiss();
            bmImage.setImageBitmap(result);
        }}
    

    【讨论】:

    • @Rana Osama 试过这个代码???
    • 还没有,但现在会处理它...非常感谢您的帮助 =)
    • 我用过。效果很好!谢谢。
    • 简短而优美的答案。非常感谢!正是我想要的!
    【解决方案2】:

    使用以下代码从 url 下载图像并将图像显示到 imageview 中,这将解决您的问题。

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
    
        ImageView mImgView1;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
    
            mImgView1 = (ImageView) findViewById(R.id.mImgView1);
            String url = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg";
            BitmapFactory.Options bmOptions;
            bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;
            Bitmap bm = loadBitmap(url, bmOptions);
            mImgView1.setImageBitmap(bm);
        }
    
        public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
            Bitmap bitmap = null;
            InputStream in = null;
            try {
                in = OpenHttpConnection(URL);
                bitmap = BitmapFactory.decodeStream(in, null, options);
                in.close();
            } catch (IOException e1) {
            }
            return bitmap;
        }
    
        private static InputStream OpenHttpConnection(String strURL)
                throws IOException {
            InputStream inputStream = null;
            URL url = new URL(strURL);
            URLConnection conn = url.openConnection();
    
            try {
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setRequestMethod("GET");
                httpConn.connect();
    
                if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    inputStream = httpConn.getInputStream();
                }
            } catch (Exception ex) {
            }
            return inputStream;
        }
    }
    

    【讨论】:

    • 感谢您的帮助。我有一个问题。 OpenHttpConnection(URL) 这一行有错误我需要导入 API 或库吗?
    • @RanaO​​sama 不,不需要任何 api 或库。
    • @RanaO​​sama 请查看我对导入包的更新答案。
    • 我收到以下错误 MainActivity 类型的 OpenHttpConnection(String) 方法未定义
    • 虽然这可行,但我建议您在从服务器下载时使用 AsyncTask。绕过 StrictMode.ThreadPolicy 不是一个好主意。
    【解决方案3】:

    您的图片链接无效。尝试使用浏览器访问它,您会发现。那是你的问题。

    【讨论】:

    • +1 用于实际检查链接
    • @ Gautam K @Lazy Ninja 链接有效加载图像“虽然它的标记没有图像它的图像”
    • 链接有效。你遇到过什么问题吗? @懒惰?
    • 是的,有问题。用这个更改网址并检查upload.wikimedia.org/wikipedia/commons/5/57/Galunggung.jpg
    • 你的链接也失效了。我确定 URL 不是问题。加载图片或获取网址内容时出现问题
    猜你喜欢
    • 2015-06-22
    • 1970-01-01
    • 2016-07-17
    • 2014-05-06
    • 2014-03-18
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    相关资源
    最近更新 更多