android 异步图片处理 工具类
16lz
2021-12-22
工具代码
public class AsyncUploadImage extends AsyncTask<Object, Object, Object> {private static final String TAG = "AsyncUploadImage ";ImageView iv;private HttpURLConnection connection;private InputStream is;private Bitmap bitmap;public AsyncUploadImage(ImageView mImageView) {iv = mImageView;}@Overrideprotected Object doInBackground(Object... params) {URL url;try {url = new URL((String) params[0]);connection = (HttpURLConnection) url.openConnection();connection.setDoInput(true);connection.connect();is = connection.getInputStream();bitmap = BitmapFactory.decodeStream(is);is.close();} catch (Exception e) {e.printStackTrace();} finally {try {if (is != null) {is.close();}if (connection != null) {connection.disconnect();}} catch (IOException e) {e.printStackTrace();}}return bitmap;}@Overrideprotected void onPostExecute(Object result) {super.onPostExecute(result);if (null != result) {iv.setImageBitmap((Bitmap) result);Log.i(TAG, "image download ok!!!");}else {iv.setBackgroundResource(R.drawable.shuben1);Log.i(TAG, "image download false!!!");}}}
具体使用的代码
new AsyncUploadImage(itemIcon).execute("http://temp/file/book/images/1325310017.jpg");//http://temp/file/book/images/1325310017.jpg -> (this is your image url..)
更多相关文章
- Android网格布局实现--recyclerview
- Android(安卓)代码混淆异常 transformClassesAndResourcesWithPr
- Android(安卓)Studio 编译不通过,报错“找不到org.apache.http。
- Android动态刷新listview中的数据
- android 数据库工具类MyDbHelper
- android WiFi 开关代码
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用