- 头像上传实体类
/**
* 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布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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.support.v7.widget.Toolbar
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="主页"
android:textColor="#ffffff"
android:textSize="20sp" />
</android.support.v7.widget.Toolbar>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrlv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
4.头像上传布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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.support.v7.widget.Toolbar
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="图片上传"
android:textColor="#ffffff"
android:textSize="20sp" />
</android.support.v7.widget.Toolbar>
<ImageView
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" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="图片上传" />
</LinearLayout>
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);
}
}