位置: 编程技术 - 正文
推荐整理分享Android 从网络下载图片OOM(安卓数据网络),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:安卓数据网络,android获取网速,android 网络连接,android网络下载,android 网络连接,android网络下载,android 网络连接,android获取网络数据,内容如对您有帮助,希望把文章链接给更多的朋友!
最近有个需求,从网络下载图片,但不能使用imageloader,因为iimageloader使用缓存机制,同一路径下的图片如果跟换怎不会刷新替换。所以必须自己下载解析。但遇到OOM的问题,如果用BitmapFactory.decodeStream 则会返回null,所以我采用的办法是先将图片下载下来,再计算图片宽高,最后绑定图片后将图片删除的办法。话不多说,先上代码
//path为图片的网络路径
private Bitmap getHttpBitmap(String path) {try {URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout();conn.setRequestMethod("GET");if (conn.getResponseCode() == ) {InputStream is = conn.getInputStream();//获取图片路径String[] names = path.split("/");String name = names[names.length-1];//保存图片到本地FileOutputStream outStream = new FileOutputStream(new File(Environment.getExternalStorageDirectory() "/" name));byte[] buffer = new byte[];int len = -1;while ((len = is.read(buffer)) != -1) {outStream.write(buffer, 0, len);}outStream.close();//获取图片宽高BitmapFactory.Options opts = new BitmapFactory.Options();opts.inJustDecodeBounds = true;BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() "/" name , opts);int imageWidth = opts.outWidth;int imageHeight = opts.outHeight;Log.i("imageWidth", String.valueOf(imageWidth));Log.i("imageHeight", String.valueOf(imageHeight));//计算缩放比例,此处为默认,也可以获取控件宽高进行计算int widthscale = imageWidth / ;int heightscale = imageHeight / ;int scale = widthscale > heightscale ? widthscale: heightscale;Log.i("scale", String.valueOf(scale));//开始真正获取图片,进行压缩后的图片opts.inJustDecodeBounds = false;opts.inSampleSize = scale;localerweima =Environment.getExternalStorageDirectory() "/" name;Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() "/" name , opts);Log.i("bitmap1", String.valueOf(bitmap));is.close();Message msg = Message.obtain();msg.what = 1;msg.obj = bitmap;handler.sendMessage(msg);return bitmap;}return null;} catch (Exception e) {Log.i("e.toString();", e.toString());return null;}}
//这是handler绑定图片并删除文件的操作
Message msg = new Message();Handler handler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case 1: // 二维码Bitmap bm = (Bitmap) msg.obj;tv_erweima.setImageBitmap(bm);new File(localerweima).delete(); //每次绑定完成后删除本地文件break;default:break;}};};
Android5.0网络之socket接口的改变 socket是网络编程的基础,Android中socket接口定义在bionic里面,Android5.0后,为了更好使用fwmark,用netd中的函数实现部分代替了bionic的socket实现。上代码bioni
Android学习【RelativeLayout控件设置宽 高】 privateRelativeLayoutrelativeLayout;relativeLayout=(RelativeLayout)findViewById(R.id.relativeLayout);player_View.getLayoutParams().height=;player_View.getLayoutParams().width=;
android 实现加载动画对话框 先来两张效果图自定义对话框:publicclassLoadingProgressDialogextendsProgressDialog{privateAnimationDrawablemAnimation;privateContextmContext;privateImageViewmImageView;privateStringmLoadi
标签: 安卓数据网络
本文链接地址:https://www.jiuchutong.com/biancheng/382180.html 转载请保留说明!上一篇:Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent错误的记录。
友情链接: 武汉网站建设