主方法:
package com.example.ningac.ningancheng20171220;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.ningac.ningancheng20171220.presenter.Presenter;
import com.example.ningac.ningancheng20171220.util.ImageUtils;
import com.example.ningac.ningancheng20171220.view.IView;
import com.facebook.drawee.view.SimpleDraweeView;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
public class MainActivity extends AppCompatActivity implements IView{
private boolean flage=true;
private Unbinder bind;
@BindView(R.id.sim)
SimpleDraweeView sim;
@BindView(R.id.back)
TextView back;
@BindView(R.id.lin)
LinearLayout lin;
@BindView(R.id.cancel)
TextView cancel;
@BindView(R.id.take_photo)
TextView photo;
@BindView(R.id.album)
TextView album;
private Presenter presenter;
private File tempFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bind = ButterKnife.bind(this);
presenter = new Presenter();
presenter.attach(this);
}
@OnClick({R.id.sim,R.id.back,R.id.cancel,R.id.take_photo,R.id.album})
public void setclick(View v){
switch (v.getId()){
case R.id.sim:
flage=false;
lin.setVisibility(View.VISIBLE);
break;
case R.id.back:
finish();
break;
case R.id.take_photo:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 判断存储卡是否可以用,可用进行存储
if (hasSdcard()) {
tempFile = new File(Environment.getExternalStorageDirectory(), "a123.jpg");
// 从文件中创建uri
Uri uri = Uri.fromFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
startActivityForResult(intent, 100);
break;
case R.id.album:
Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
intent1.setType("image/*");
startActivityForResult(intent1, 200);
break;
case R.id.cancel:
flage=true;
lin.setVisibility(View.GONE);
break;
}
}
private boolean hasSdcard() {
//判断SD卡手否是安装好的 media_mounted
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
private void crop(Uri uri) {
// 裁剪图片意图
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪后输出图片的尺寸大小
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("outputFormat", "JPEG");// 图片格式
intent.putExtra("noFaceDetection", true);// 取消人脸识别
intent.putExtra("return-data", true);
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT
startActivityForResult(intent, 300);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==200){
Uri uri = data.getData();
crop(uri);
}else if(requestCode==100){
crop(Uri.fromFile(tempFile));
Log.d("zzz",100+"");
}else if(requestCode==300){
// 从剪切图片返回的数据
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");
/**
* 获得图片
*/
setImgByStr( bitmap);
}
try {
// 将临时文件删除
tempFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 上传头像
*/
public void setImgByStr(Bitmap bitmap) {
if(bitmap != null){
// 拿着imagePath上传了
// ...
}
String imagePath = ImageUtils.savePhoto(bitmap, Environment
.getExternalStorageDirectory().getAbsolutePath(), String
.valueOf(System.currentTimeMillis()));
Log.d("zxz","imagePath:"+imagePath);
if(imagePath!=null){
File file=new File(imagePath);//将要保存图片的路径
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/png"), file);
MultipartBody.Part photo = MultipartBody.Part.createFormData("file", file.getName(), photoRequestBody);
presenter.getdta("71",photo);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
bind.unbind();
}
@Override
public void success(String message) {
Log.d("zzz",message);
}
@Override
public void failed(String message) {
Log.d("zzz",message);
}
}
img工具类:
package com.example.ningac.ningancheng20171220.util;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Ning.A.C on 2017/12/21.
*/
public class ImageUtils {
/**
* Save image to the SD card
*
* @param photoBitmap
* @param photoName
* @param path
*/
public static String savePhoto(Bitmap photoBitmap, String path,
String photoName) {
String localPath = null;
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File photoFile = new File(path, photoName + ".png");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100,
fileOutputStream)) { // 转换完成
localPath = photoFile.getPath();
fileOutputStream.flush();
}
}
} catch (FileNotFoundException e) {
photoFile.delete();
localPath = null;
e.printStackTrace();
} catch (IOException e) {
photoFile.delete();
localPath = null;
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
fileOutputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return localPath;
}
/**
* 转换图片成圆形
*
* @param bitmap 传入Bitmap对象
* @return
*/
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width,
height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
}
}
Retrofit写法:
@Multipart
@POST("upload?")
Flowable<Bean> uploading(@Query("uid") String uid, @Part MultipartBody.Part file);