使用rxjava2.0根据url获取bitmap,并显示到imageview上
来源:互联网 发布:arctime 字幕制作软件 编辑:程序博客网 时间:2024/05/19 04:28
/*retrofit的引入*/ compile 'com.squareup.retrofit2:retrofit:2.2.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' /*retrofit对rx支持的引入*/ compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0' /*rx的引入*/ compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.0.9'
对于rx的引入,只需要最后两个即可,其他的是为了retrofit的使用
例子中的第二个参数,由于是项目使用。所以参数不具备通用性,但是方法就这样
private void showImage(final String imageUrl, final JCVideoPlayerStandard jcVideoPlayerStandard) { Observable.create(new ObservableOnSubscribe<Bitmap>() { @Override public void subscribe(@io.reactivex.annotations.NonNull ObservableEmitter<Bitmap> e) throws Exception { //e.onNext(BitmapFactory.decodeResource(getResources(), R.drawable.shoucang)); e.onNext(GetImageInputStream(imageUrl)); } }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Bitmap>() { @Override public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) { } @Override public void onNext(@io.reactivex.annotations.NonNull Bitmap s) { jcVideoPlayerStandard.thumbImageView.setImageBitmap(s); } @Override public void onError(@io.reactivex.annotations.NonNull Throwable e) { } @Override public void onComplete() { } }); } /** * 获取网络图片 * * @param imageurl 图片网络地址 * @return Bitmap 返回位图 */ public Bitmap GetImageInputStream(String imageurl) { URL url; HttpURLConnection connection = null; Bitmap bitmap = null; try { url = new URL(imageurl); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(6000); //超时设置 connection.setDoInput(true); connection.setUseCaches(false); //设置不使用缓存 InputStream inputStream = connection.getInputStream(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } return bitmap; }
阅读全文
0 0
- 使用rxjava2.0根据url获取bitmap,并显示到imageview上
- android URL获取图片显示到ImageView 控件上
- 根据url路径获取图片并显示到ListView中
- 通过图片的url获取Bitmap对象,给Imageview使用
- 根据url获取Bitmap对象
- 举例说明android中Bitmap、ListView以及ImageView的综合使用---显示图片并获取图片的宽和高
- 调相册绑定到ImageView上(并根据imageView的大小剪切图片)
- android 获取网络图片并在Imageview上显示
- android 获取网络图片并在Imageview上显示
- 获取sd卡中的图片URL并显示,同时使用ImageView把选择的图片展现出来
- 从网络获取图片保存到sdcard,以及加载为bitmap显示到ImageView
- 根据图片的网络URL获取Bitmap
- Android—根据URL获取Bitmap图片
- 根据URL获取图片的bitmap
- 根据图片url获取bitmap对象,并保存至sd卡
- 根据URL加载图片并付给bitmap(微信分享中使用)
- 获取服务器Url图片资源,并显示在ImageView中 Android
- Android 通过url获取网络图片并显示在ImageView中
- 手机安装app总是显示未安装
- 利用openssl进行RSA加密解密实例
- Phonegap项目创建 编译 安装 运行
- 概率论中指数分布介绍及C++11中std::exponential_distribution的使用
- 获取文件版本
- 使用rxjava2.0根据url获取bitmap,并显示到imageview上
- 敏感词Util
- 实战
- 【笔记】java命令如何运行jar包
- git学习记录
- 一位资深程序员大牛给予Java初学者的学习路线建议
- CSS Mastery2(10.5.4-end)@font-face&cufon&&Ajax&jQuery增强交互性&黑画(5-another road)
- Android开发中那些有用的方法
- Java中内存中的Heap(堆)、Stack(栈)与程序运行的关系