0

URLから画像を取得して画像ビューに表示できるアプリを開発しています...今、このコードを試してみました....\

コード

**
    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);

            }
        }
    }

oncreate ::

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

UPDATE2

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

しかし、logcat.iで印刷するとBMPがNullになり、画像のURLも確認して、ブラウザでうまくいきます。しかし、ここで重大な問題が発生したため、 NULL になっています。これを手伝ってもらえますか....

4

2 に答える 2

0

here で述べたように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 に答える
0

非同期イメージ ローダー用のこのライブラリを試してください。

利用可能なサンプルコードとライブラリの数があり、それらのいくつかは以下のとおりです

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

https://github.com/thest1/LazyList

于 2012-06-26T11:35:42.447 に答える