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 になっています。これを手伝ってもらえますか....