1.在安卓端选择从相册还是相机中获取图片
2.用data获取图片,再用crop裁剪图片,并设置头像
3.将Bitmap格式转换为String格式
4.用okhttp post/get的方式向服务器发送String(如果直接用后缀的方式,一般情况下会报错,因为服务器的请求设置了大小限制,在tomacat的话,在server.xml中配置最大限制就可以了)
5.服务端接收到String,并将其保存到数据库
//全局静态变量
private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
// 激活系统图库,选择一张图片
Intent intent1 = new Intent(Intent.ACTION_PICK);
intent1.setType("image/*");
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY
startActivityForResult(intent1, PHOTO_REQUEST_GALLERY);
//激活照相机,省略
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_REQUEST_GALLERY) {
// 从相册返回的数据
if (data != null) {
// 得到图片的全路径
Uri uri = data.getData();
crop(uri);
}
} else if (requestCode == PHOTO_REQUEST_CUT) {
// 从剪切图片返回的数据
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");
/**
* 获得图片
*/
imageView1.setImageBitmap(bitmap);
//转化格式
String imgString=changeToString(bitmap);
String address="androidtest?imgstring="+imgString;
//上传服务器
serverPostPicture(address);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
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, PHOTO_REQUEST_CUT);
}
private String changeToString(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
//第二步:利用Base64将字节数组输出流中的数据转换成字符串String
byte[] byteArray = byteArrayOutputStream.toByteArray();
String imageString = new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
if(imageString!=null){
return imageString;
}
else {
return "";
}
}
private void serverGetPicture(String address){
HttpUtil.sendOkHttpRequest(address, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView2.setText("失败");
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String responsedata=response.body().string();
if(response!=null){
runOnUiThread(new Runnable() {
@Override
public void run() {
changeToBitmap(responsedata);
}
});
}
}
});
}
public class HttpUtil {
//final static String IP="http://192.168.137.1:8080/caidao/"; //手机连电脑热点,查看本地连接
final static String IP="http://192.168.43.175:8080/caidao/";//电脑连手机热点,查看wlan连接
public static void sendOkHttpRequest(String address,okhttp3.Callback callback){
address =IP+address;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(address).build();
client.newCall(request).enqueue(callback);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String imgstring=request.getParameter("imgstring");//获得字符串
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
System.out.println(“text”);//服务端输出text
//上传数据库代码省略
PrintWriter out = response.getWriter();
out.print("上传成功");//利用response返回给android端
out.flush();
out.close();
response.getWriter().append("Served at: ").append(request.getContextPath());
}
www.shieldwen.com 博主自己的开发的个人网站,欢迎来踩!