android开发之上传头像

一、调起相机

  • 所需权限有:相机权限(android.permission.CAMERA),文件存取权限(android.permission.WRITE_EXTERNAL_STORAGE、android.permission.READ_EXTERNAL_STORAGE)

public static File openCamera(Activity context) {
File cameraSavePath = new File(Environment.getExternalStorageDirectory().getPath()
+ File.separator + Environment.DIRECTORY_DCIM + “/Camera/” + System.currentTimeMillis() + “.jpg”);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(context, context.getPackageName() + “.fileProvider”, cameraSavePath);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.fromFile(cameraSavePath);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
context.startActivityForResult(intent, 1);
return cameraSavePath;
}

二、调起相册

  • 所需权限有:文件存取权限(android.permission.WRITE_EXTERNAL_STORAGE、android.permission.READ_EXTERNAL_STORAGE)

public static void openAlbum(Activity context) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, “image/*”);
context.startActivityForResult(intent, 2);
}

三、裁剪图片

public static Uri startCrop(Activity context, Uri uri) {
Intent intent = new Intent(“com.android.camera.action.CROP”);
intent.setDataAndType(uri, “image/*”);
intent.putExtra(“circleCrop”, true);
intent.putExtra(“scale”, true);
intent.putExtra(“aspectX”, 1);
intent.putExtra(“aspectY”, 1);
intent.putExtra(“outputX”, ScreenHelper.dp2Px(context, 60));
intent.putExtra(“outputY”, ScreenHelper.dp2Px(context, 60));
intent.putExtra(“return-data”, false);//是否以 Bitmap形式返回(存在内存中,过大时可能会 OOM)
File outFile = new File(Environment.getExternalStorageDirectory().getPath()
+ File.separator + Environment.DIRECTORY_DCIM + “/Camera/crop_” + System.currentTimeMillis() + “.jpg”);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
Uri out = Uri.fromFile(outFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, out);//裁剪后输出路径(保存)
intent.putExtra(“outputFormat”, Bitmap.CompressFormat.JPEG.toString());
context.startActivityForResult(intent, 3);
return out;
}
//注意:裁剪后的文件输出URI 只用Uri.fromFile(outFile)即可

四、调起系统页面后的回调:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//相机回调
if (requestCode == 1 && resultCode == RESULT_OK) {
File cameraSavePath = cameraFile;
Uri uri;
//适配 7.0及以上系统
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(this, getPackageName() + “.fileProvider”, cameraSavePath);
} else {
uri = Uri.fromFile(cameraSavePath);
}
mCropUri = startCrop(this, uri);
} else if (requestCode == 2 && resultCode == RESULT_OK) {
//相册回调
if (data != null)
mCropUri = startCrop(this, data.getData());
} else if (requestCode == 3 && resultCode == RESULT_OK && mCropUri != null) {
//裁剪后
File file = new File(mCropUri.getEncodedPath());
String photoPath;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
photoPath = String.valueOf(file);
else
photoPath = Uri.fromFile(file).getEncodedPath();
uploadHeader(photoPath);
}
}

五、上传头像

  • 使用的是 Retrofit框架

public void uploadHeader(String filePath) {
File file = new File(filePath);
RequestBody id = RequestBody.create(MediaType.parse(“multipart/form-data”), SpUtil.getCustomerId(mContext));
RequestBody requestBody = RequestBody.create(MediaType.parse(“image/jpg”), file);
MultipartBody.Part part = MultipartBody.Part.createFormData(“file”, file.getName(), requestBody);

}

@Multipart
@POST(Api.uploadHeadImg)
Call uploadHeadImage(@Part(“id”) RequestBody id, @Part() MultipartBody.Part file);

你可能感兴趣的:(android开发系列,android)