android okhttp 上传gif图片,1.OkHttp头像上传

头像上传实体类

/**

* code : 200

* res : 上传文件成功

* data : {"url":"http://yun918.cn/study/public/uploadfiles/img/image1.png"}

*/

private int code;

private String res;

private DataBean data;

public int getCode() {

return code;

}

public void setCode(int code) {

this.code = code;

}

public String getRes() {

return res;

}

public void setRes(String res) {

this.res = res;

}

public DataBean getData() {

return data;

}

public void setData(DataBean data) {

this.data = data;

}

public static class DataBean {

/**

* url : http://yun918.cn/study/public/uploadfiles/img/image1.png

*/

private String url;

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

}

2.上传头像工具类

//sd卡是否存在

public static boolean sdCardIsAvailable() {

if(Environment.getExternalStorageState()

.equals(Environment.MEDIA_MOUNTED)) {

return true;

}

return false;

}

//获取sd卡路径

public static String getSDPath() {

File sdDir = null;

if (sdCardIsAvailable()) {

//获取根目录

sdDir = Environment.getExternalStorageDirectory();

}

return sdDir.toString();

}

//byte[]转bitmap

public static Bitmap getBitmapByBytes(byte[] bytes, BitmapFactory.Options options) {

if (bytes != null) {

if (options != null) {

return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);

} else {

return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

}

}

return null;

}

//Bitmap转byte[]

public static byte[] getByteByBmp(Bitmap bmp) {

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

bmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

return outputStream.toByteArray();

}

3.文件上传MainActivity布局

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.example.a19950115.kaoshi.MainActivity">

android:id="@+id/toolBar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#28A5DF"

android:minHeight="?android:actionBarSize"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="主页"

android:textColor="#ffffff"

android:textSize="20sp" />

android:id="@+id/xrlv"

android:layout_width="match_parent"

android:layout_height="match_parent" />

4.头像上传布局

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.example.a19950115.kaoshi.TuPian.Activity_TouXiang">

android:id="@+id/toolBar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#28A5DF"

android:minHeight="?android:actionBarSize"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="图片上传"

android:textColor="#ffffff"

android:textSize="20sp" />

android:id="@+id/img"

android:layout_width="200dp"

android:layout_height="200dp"

android:layout_gravity="center_horizontal"

android:layout_marginTop="10dp"

android:src="@mipmap/ic_launcher" />

android:id="@+id/tv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp" />

android:id="@+id/tv1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp" />

android:id="@+id/login"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="图片上传" />

5.头像上传MainActivity步骤

public class Activity_TouXiang extends AppCompatActivity implements View.OnClickListener {

private Toolbar toolBar;

private ImageView img;

private TextView tv;

private TextView tv1;

private Button login;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity__tou_xiang);

initView();

}

private void initView() {

//获取Toolbar控件

toolBar = (Toolbar) findViewById(R.id.toolBar);

toolBar.setTitle("");

setSupportActionBar(toolBar);

toolBar.setNavigationIcon(R.drawable.aa);

toolBar.setNavigationOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

finish();

}

});

//获取控件

img = (ImageView) findViewById(R.id.img);

tv = (TextView) findViewById(R.id.tv);

tv1 = (TextView) findViewById(R.id.tv1);

login = (Button) findViewById(R.id.login);

//原始图片地址

tv.setText(Utils.getSDPath() + "/r.jpg");

//加载一张圆形图片

RequestOptions options = RequestOptions.circleCropTransform()

.placeholder(R.mipmap.ic_launcher)

.diskCacheStrategy(DiskCacheStrategy.RESOURCE)

.skipMemoryCache(true);

Glide.with(this).load(R.drawable.f).apply(options).into(img);

//按钮点击事件监听

login.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.login:

uploadImage();

break;

}

}

//文件上传

private void uploadImage() {

File file = new File(Utils.getSDPath() + "/r.jpg");

if (file.exists()) {

//上传文件的格式

String format = "image/jpg"; //image/jpg image/gif multipart/form-data

RequestBody requestBody = RequestBody.create(MediaType.parse(format), file);

//设置上传文件的内容

RequestBody body = new MultipartBody.Builder()

.setType(MultipartBody.FORM)

.addFormDataPart("key", "image110") //传参数

.addFormDataPart("file", file.getName(), requestBody)

.build();

OkHttpClient okHttpClient = new OkHttpClient.Builder()

.build();

//设置上传地址和内容

Request request1 = new Request.Builder()

.url("http://yun918.cn/study/public/file_upload.php")

.post(body)

.build();

Call call = okHttpClient.newCall(request1);

call.enqueue(new Callback() {

@Override

public void onFailure(Call call, IOException e) {

Log.i("onFailure", e.toString());

}

@Override

public void onResponse(Call call, Response response) throws IOException {

String result = response.body().string();

Gson gson = new Gson();

final ImageBean imageBean = gson.fromJson(result, ImageBean.class);

if (imageBean.getCode() == 200) {

runOnUiThread(new Runnable() {

@Override

public void run() {

updateImageView(imageBean);

}

});

}

Log.i("result", result);

}

});

}

}

//更新本地imageview图片

private void updateImageView(ImageBean imageBean) {

tv1.setText(imageBean.getData().getUrl());

RequestOptions options = new RequestOptions().circleCrop();

Glide.with(this).load(imageBean.getData().getUrl()).apply(options).into(img);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值