使用okhttp异步下载图片,保存到本地,并在系统相册中显示

//首先需要创建一个OkHttpClient实例

private OkHttpClient mOkHttpClient = new OkHttpClient();
private Handler mDelivery = new Handler(Looper.getMainLooper());

/**
* 下载图片并返回结果
/

private void loadImage(final String url, final ResultCallBack callBack) {
        Request request = new Request.Builder().url(url).build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                mDelivery.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onError(request, e);
                    }

                    @Override
                    public void onResponse(Response response) {
                        InputStream is = null;
                        try {
                            is = response.body.byteStream();
                            is.reset();
                            BitmapFactory.Options ops = new BitmapFactory.Options();
                            ops.inJustDecodeBounds = false;
                            final Bitmap bm = BitmapFactory.decodeStream(is, null, ops);
                            mDelivery.post(new Runnable() {
                                @Override
                                public void run() {
                                    callBack.onResponse(bm);
                                }
                            });
                        } catch (Exception e) {
                        } finally {
                            if (is != null) {
                                is.close();
                            }
                        }

                    }

                });
            }
        });
    }

/*将获取到的bitmap保存到本地,并在相册中有展示/

private void onSaveBitmap(Bitmap mBitmap, Context context) {
        new Thread(new Runnable() {
            @Ovrride
            public void run() {
                String photoPath = Environment.getExternalStorageDirectory
                        .getAbsolutePath +/test / image / +test.jpg;
                //创建文件对象,用来存储新的图像文件
                File file = new File(photoPath);
                //创建文件
                file.creatNewFile();
                //定义文件输出流
                FileOutputStream fOut = new FileOutputStream(file);
                //将bitmap存储为jpg格式的图片
                mBitmap.compess(Bitmap.CompessFormat.JPEG, 100, fout);
                fout.flush();//刷新文件流
                fout.close();

                //文件存储已经完毕,保存的图片没有加入到系统图库中
                //,此时需要发送广播,刷新图库,很简单几行代码搞定
                Intent intent = 
                new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri uri = Uri.fromFile(new File(photoPath));
                intent.setDate(uri);
                context.sendBroadcast(intent)

            }
        }).start();
    }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值