Android裁剪头像上传到远程服务器

  梳理一下从中学习的知识点。欢迎大家一起交流。

 android从图库选择图片或者拍照后对图片进行裁剪,裁剪后上传到腾讯云服务器。

// 从图库选择按钮 public void tuku(View v) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); // intent.setType("image/*"); startActivityForResult(intent, tuku); }

定义一个标记变量:private static int tuku = 2;

选择后执行onActivityResult事件:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  //如果点击的从图库选择的按钮

  if (requestCode == tuku) {

                    //判断是否选择了图片 if (data == null) { return; }

                       //在这里得到图片的url,这里得到的是contenturl需要将其转化为fileurl; Uri uri = data.getData(); try { Uri fileuri = conver(uri);//url转化方法 startZoom(fileuri);//调用系统裁剪头像 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (requestCode == caijian) { if (data == null) { return; } Bundle bundle = data.getExtras(); Bitmap bitmap = bundle.getParcelable("data"); round.setImageBitmap(bitmap);//将图片绑定控件

}

}

///前方高能////////////////////////

// 将bitamp转化为file类型的uri private Uri savebitmap(Bitmap bitmap) { File file = new File(Environment.getExternalStorageDirectory() + "/touxiang.ceshi"); if (!file.exists()) { file.mkdir(); } File ima = new File(file.getAbsoluteFile() + "test.png"); try { FileOutputStream fileOutputStream = new FileOutputStream(ima); bitmap.compress(Bitmap.CompressFormat.PNG, 95, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); return Uri.fromFile(ima); } catch (FileNotFoundException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } // 将contenturi转化为fileuri private Uri conver(Uri uri) throws IOException { InputStream inputStream = null; try { inputStream = getContentResolver().openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); inputStream.close(); return savebitmap(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } // 图像裁剪 private void startZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 500); intent.putExtra("outputY", 500); intent.putExtra("return-data", true); startActivityForResult(intent, caijian); } // 上传监听事件 public void up(View v) { Bitmap bm = ((BitmapDrawable) round.getDrawable()).getBitmap(); if (bm==null) { Toast.makeText(MainActivity.this, "未选择头像", 0).show(); } ByteArrayOutputStream stream = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 90, stream); byte [] bytes=stream.toByteArray(); String ima=new String(Base64.encodeToString(bytes, Base64.DEFAULT)); AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); RequestParams params=new RequestParams();//保存传入的参数 params.add("img", ima); asyncHttpClient.post("服务端url", params,new AsyncHttpResponseHandler() { @Override public void onSuccess(int arg0, Header[] arg1, byte[] arg2) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "上传成功", 0).show(); } @Override public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "上传失败", 0).show(); } }); };

/******************************服务端代码就比较简单了****************************//

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>上传图片至服务器</title> </head> <body>    <?php     $filename=date("YmdHis");          $file=fopen($filename.".png", "w"); $data=base64_decode($_POST['img']); fwrite($file, $data); fclose($file);    ?> </body> </html>

欢迎大家加我的qq:1306214077和我一起交流,尊重原创!

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券