一.在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);
}
};