0

我正在开发一个应用程序,我可以在其中从 URL 获取图像并将其显示在 imageview 中...现在我已经尝试通过此代码....\

代码

**
    private Bitmap LoadImage(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) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    private 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) {
            ex.printStackTrace();
        }
        return inputStream;
    }

    /*
     * private class FetchImageTask extends AsyncTask<String, Integer, Bitmap> {
     * 
     * @Override protected Bitmap doInBackground(String... arg0) { Bitmap b =
     * null; try { b = BitmapFactory.decodeStream((InputStream) new
     * URL(arg0[0]).getContent()); } catch (MalformedURLException e) {
     * e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
     * return b; } }
     */

    private class NearByScreenTask extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(context);
        Bitmap BMP = null;

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage("Please Wait...");

            this.dialog.show();
            // put your code which preload with processDialog
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                // TODO Auto-generated method stub
                BitmapFactory.Options bmOptions;
                bmOptions = new BitmapFactory.Options();
                bmOptions.inSampleSize = 1;
                BMP = LoadImage(strURL, bmOptions);

            } catch (Exception e) {
                Log.e(TAG, "ERROR" + e);
            }
            return null;
        }

        @Override
        protected void onPostExecute(final Void unused) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
                Log.e(TAG, "BMP is :: ---" + BMP);
                image.setImageBitmap(BMP);

            }
        }
    }

创建::

image.setImageResource(android.R.color.transparent);
            Log.i(TAG, "item.getImageURL()" + item.getImageURL());
            strURL = item.getImageURL();
            new NearByScreenTask().execute();

更新2

    BMP = BitmapFactory.decodeStream(new java.net.URL(strURL).openStream());
image.setImageBitmap(BMP);

但是当我在 logcat 中打印它时,BMP 让我为 Null。我还检查了图像 url 并且它在浏览器中运行良好。但是这里提出了一些关键问题,所以它变成了 NULL ,你能帮我解决这个问题吗......

4

2 回答 2

0

试试这个库的异步图像加载器。

有许多示例代码和库可用,其中一些如下

https://github.com/nostra13/Android-Universal-Image-Loader

https://github.com/thest1/LazyList

于 2012-06-26T11:35:42.447 回答
0

如此处所述,您应该尝试将OpenHttpConnection()方法更改为:

private InputStream OpenHttpConnection(String strURL) throws IOException {
    HttpGet httpRequest = new HttpGet(URI.create(strURL));
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    return bufHttpEntity.getContent();
}
于 2012-06-26T11:46:39.420 回答