手机客户端修剪图片并上传图片到服务端比较经常碰到,比如设置用户头像以及裁剪图像大小的功能,可以打开相机拍照 选择相册 裁剪图片,然后获取uri直接转路径:安卓7.0以上兼容
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import java.io.File;
public class PhotoUtils{
private PhotoUtils(){}
//uri转路径
public static String uri2path(final Context context,String uristr) {
if (null == uristr) return null;
Uri selectUri=Uri.parse(uristr);
String selectPath=selectUri.getPath();//路径或ID
if(uristr.startsWith("content://com.android.providers.downloads.documents")){//下载目录
if (selectPath.contains("raw:")){
return selectPath.substring(selectPath.indexOf("raw:")+4);
}
String[] cont={"content://downloads/public_downloads","content://downloads/my_downloads","content://downloads/all_downloads"};
for (int i=0;i<cont.length;i++) {
Uri myuri = Uri.parse(cont[i] + selectPath.substring(selectPath.lastIndexOf("/")));
try{
Cursor cursor=context.getContentResolver().query(myuri,null,null,null,null);
if (null!=cursor) {
selectUri=myuri;
i=10000;
cursor.close();
}
}catch (Exception e){
}
}
}else if(uristr.startsWith("content://com.android.providers.media.documents")){//媒体目录
if (selectPath.contains("image")){
selectUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if (selectPath.contains("video")){
selectUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if (selectPath.contains("audio")){
selectUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
String[] split = selectPath.split(":");
selectUri = Uri.parse(selectUri.toString()+"/"+(split!=null&&split.length>1?split[1]:""));
}else if(uristr.startsWith("content://")){//普通目录
//selectUri=selectUri;
}else{
return selectPath;
}
try{
Cursor cursor=context.getContentResolver().query(selectUri,new String[]{"_data"},null,null,null);
if (null!=cursor) {
if (cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex("_data");
selectPath = cursor.getString(columnIndex);
}
cursor.close();
}
}catch (Exception e){
}
return selectPath;
}
/**
*拍照上传
*/
public static void photoGraph(Activity activity,String filePath){
File file = new File(filePath);
if ( !file.exists() ) {
file.getParentFile().mkdirs();
}
Uri uri = Uri.fromFile(file);
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);//设置SD卡读写权限
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
activity.startActivityForResult(intent,1);
}
/**
*相册文件
*/
public static void fromAlbum(Activity activity,boolean isAlbum){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(isAlbum ? "image/*" : "*/*");//设置类型
activity.startActivityForResult(intent,isAlbum ? 1 : 2);
}
/**
* 裁剪原始的图片
*/
public static void cropPhoto(Activity activity,String filePath) {
File file = new File(filePath);
if ( !file.exists() ) {
file.getParentFile().mkdirs();
}
Uri uri = Uri.fromFile(file);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// intent.putExtra("aspectX", 360);
// intent.putExtra("aspectY", 225);
// intent.putExtra("outputX", 600);
// intent.putExtra("outputY", 500);
// intent.putExtra("return-data", true);//返回bitmap可能OOM
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);//指定uri可能data空
intent.putExtra("scale", true);//黑边
intent.putExtra("scaleUpIfNeeded", true);//黑边
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
activity.startActivityForResult(intent,3);
}
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
// if (requestCode == 1 && resultCode == RESULT_OK) {
// String selectPath=PhotoUtils.uri2path(this,data==null?PATH:data.getDataString());
// BitmapUtils.getOOMBitmap(selectPath,PATH);//需压缩图片new File(PATH);
// //PhotoUtils.cropPhoto(this,PATH);//裁剪图片
// }
// if (requestCode == 2 && resultCode == RESULT_OK) {
// String selectPath=PhotoUtils.uri2path(this, data.getDataString());
// if (selectPath==null||!selectPath.contains(".mp4")){//指定格式
// ToastUtils.setToast(EducateAddActivity.this,"格式不正确");
// return;
// }
// }
// if(requestCode==3&&resultCode==RESULT_OK){
// String selectPath=PhotoUtils.uri2path(this,data==null?PATH:data.getDataString());
// BitmapUtils.getOOMBitmap(selectPath,PATH);//需压缩图片new File(PATH);
// }
//
// }
}
来 源:http://blog.csdn.net/a704755096/article/details/50619593
旋转图片:http://blog.csdn.net/bd_zengxinxin/article/details/39697597
加背景框:http://blog.csdn.net/yanzi1225627/article/details/8622257