使用Okhttp网络请求下载图片到指定文件夹
来源:互联网 发布:word 矩阵 编辑:程序博客网 时间:2024/06/16 17:28
一.在module中添加依赖
compile 'com.squareup.okhttp3:okhttp:3.6.0'
二.设置布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zhiyuan3g.myokhttpupload.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上传"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zhiyuan3g.myokhttpupload.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上传"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
三.使用okhttp下载图片到指定文件夹
OkHttpClient okHttpClient=new OkHttpClient(); Request request=new Request.Builder() .get() .url("http://pic.qiantucdn.com/58pic/17/85/35/559de1de9b223_1024.jpg") .build(); Call call=okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //将响应数据转化为输入流数据 InputStream inputStream=response.body().byteStream(); //将输入流数据转化为Bitmap位图数据 Bitmap bitmap= BitmapFactory.decodeStream(inputStream); File file=new File("/mnt/sdcard/picture.jpg"); file.createNewFile(); //创建文件输出流对象用来向文件中写入数据 FileOutputStream out=new FileOutputStream(file); //将bitmap存储为jpg格式的图片 bitmap.compress(Bitmap.CompressFormat.JPEG,100,out); //刷新文件流 out.flush(); out.close(); Message msg=Message.obtain(); msg.obj=bitmap; handler.sendMessage(msg); } });
四.如果要把下载的图片显示出来,可以在主线程中添加自定义Handler内部类
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
image.setImageBitmap((Bitmap) msg.obj);
}
};
@Override
public void handleMessage(Message msg) {
image.setImageBitmap((Bitmap) msg.obj);
}
};
3 0
- 使用Okhttp网络请求下载图片到指定文件夹
- Android使用OkHttp框架下载网络图片
- android之OkHttp使用(网络请求,上传,下载)
- 通过Src下载图片存到指定文件夹
- OkHttp网络请求的使用
- 下载文件到指定文件夹
- 请求网络下载图片
- Android使用okhttp下载图片
- 网络请求封装及使用(okhttp)
- okhttp网络请求框架的简单使用
- Android网络请求框架的使用okhttp
- Android使用Retrofit+OkHttp实现网络请求
- android网络请求okhttp的使用
- 网络请求框架okhttp的使用
- Android OkHttp 网络请求 使用详解
- Android网络请求框架使用 -- OkHttp
- OkHttp网络请求框架的使用
- 网络请求篇--RXJava+okhttp 进行网络下载操作
- ASP.NET参数化SQL语句(SQL SERVER)
- golang chan 使用例子
- Servlet操作数据库
- Sqoop fails with "Error parsing arguments for import"
- Spring入门5——事务管理
- 使用Okhttp网络请求下载图片到指定文件夹
- 字符编码介绍
- HTTP cookies 详解
- Linux中的Find 指令
- java 基础知识汇总
- 雨滴
- 侠盗猎车手5(GTA5)50辆游戏豪车现实原型
- BinarySearch二分法查找
- 导入WeChatSDK_sample_Android项目报错