如何將以下文本置於AsyncTask
?我的問題是,通過直接放置代碼,我得到一個錯誤NetworkOnMainThreadException
。我需要任何幫助。由於Android:將網址代碼放入AsyncTask
try {
URL myFileUrl = new URL ("http://.../cercanias.png");
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is);
FileOutputStream out = new FileOutputStream(getCacheDir() + "cercanias.png");
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)findViewById(R.id.imagenNitida);
imageView.setImageFile(getCacheDir() + "cercanias.png");
out.close();
if (savedInstanceState != null &&
savedInstanceState.containsKey(STATE_SCALE) &&
savedInstanceState.containsKey(STATE_CENTER_X) &&
savedInstanceState.containsKey(STATE_CENTER_Y)) {
imageView.setScaleAndCenter(savedInstanceState.getFloat(STATE_SCALE),
new PointF(savedInstanceState.getFloat(STATE_CENTER_X),
savedInstanceState.getFloat(STATE_CENTER_Y)));
}
} catch (IOException e) {
}
把它放在doInBackground? – Skynet