上传头像
来源:互联网 发布:mac ai抠图教程视频 编辑:程序博客网 时间:2024/05/29 09:11
public void head(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("设置头像"); String[] items = { "选择本地照片", "拍照" }; builder.setNegativeButton("取消", null); builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case CHOOSE_PICTURE: // 选择本地照片 Intent openAlbumIntent = new Intent( Intent.ACTION_GET_CONTENT); openAlbumIntent.setType("image/*"); startActivityForResult(openAlbumIntent, CHOOSE_PICTURE); break; case TAKE_PICTURE: // 拍照 Intent openCameraIntent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); tempUri = Uri.fromFile(new File(Environment .getExternalStorageDirectory(), "image.jpg")); // 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换 openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri); startActivityForResult(openCameraIntent, TAKE_PICTURE); break; } } }); builder.create().show();}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { // 如果返回码是可以用的 switch (requestCode) { case TAKE_PICTURE: startPhotoZoom(tempUri); // 开始对图片进行裁剪处理 break; case CHOOSE_PICTURE: startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理 break; case CROP_SMALL_PICTURE: if (data != null) { setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上 } break; } }}protected void startPhotoZoom(Uri uri) { if (uri == null) { Log.i("tag", "The uri is not exist."); } tempUri = uri; Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 设置裁剪 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 150); intent.putExtra("outputY", 150); intent.putExtra("return-data", true); startActivityForResult(intent, CROP_SMALL_PICTURE);}protected void setImageToView(Intent data) { Bundle extras = data.getExtras(); if (extras != null) { final Bitmap photo = extras.getParcelable("data"); setFile(photo); OkHttpClient okHttpClient1 = new OkHttpClient(); MultipartBody.Builder builder1 = new MultipartBody.Builder(); builder1.setType(MultipartBody.FORM); File file=new File("mnt/sdcard/mo.jpg"); builder1.addFormDataPart("uid",uid+""); System.out.println(file.getName()+file.toString()+"================================================="); builder1.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file)); Request request1 = new Request.Builder().post(builder1.build()).url(API.UPLOAD).build(); okHttpClient1.newCall(request1).enqueue(new Callback() { @Override public void onFailure(okhttp3.Call call, IOException e) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(GerenActivity.this, "网络上传失败", Toast.LENGTH_SHORT).show(); } }); } @Override public void onResponse(okhttp3.Call call, final Response response) throws IOException { runOnUiThread(new Runnable() { @Override public void run() { if (response.isSuccessful()){ try { head.setImageBitmap(photo); String string = response.body().string(); System.out.println("fileuploadsuccess:" + string); Toast.makeText(GerenActivity.this, "上传成功", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } } }); } }); }}private void setFile(Bitmap photo) { File file=new File("mnt/sdcard/mo.jpg"); try { BufferedOutputStream bout=new BufferedOutputStream(new FileOutputStream(file)); photo.compress(Bitmap.CompressFormat.JPEG,100,bout); bout.flush(); bout.close(); } catch (Exception e) { e.printStackTrace(); }}
阅读全文
0 0
- 上传头像
- 上传头像
- 上传头像
- 上传头像
- 头像上传
- 上传头像
- 头像上传
- 头像上传
- 头像上传
- 上传头像
- 上传头像
- 头像上传
- 头像上传
- 上传头像
- 头像上传
- 上传头像
- 上传头像
- 上传头像
- 数据结构-顺序表(3)顺序表的建立、销毁、置空
- 杭电 2136-Largest prime factor
- Python chapter 2&3 learning notes
- 随机森林
- 阿里云CentOS 7下配置nginx环境
- 上传头像
- Android Studio中上传项目到码云时的一些问题
- Linux教程【3】VMWare的三种网络连接方式-远程登录管理工具SecureCRT-Winscp文件拷贝工具
- Apache httpd.conf配置文件 3(虚拟主机)
- HTML,angularJS表格内容排序,点击删除,删除当前,输入内容显示包含内容的数据
- mysql服务性能优化—my.ini配置说明详解 16G内存
- vue项目根目录下index.html中的id="app",与src目录下的App.vue中的id="app"为什么不会冲突
- 从零开始前端学习[40]:css3中的resize,user-select属性,多列布局特性,怪异盒子模型,倒影
- rtklib使用kalman滤波进行高精度相对定位原理