private static final int PHOTO_PICKED_WITH_DATA = 3021;
//用于保存大图片位置的uri
private static Uri mCropUri;
public void setWallPaper(View v) {
//获取图片在手机内存的地址
File file = ImageLoader.getInstanse(this).getImageFile(picUrl);
//给保存位置设一个位置,并转换成uri
mCropUri = Uri.fromFile( new File(file.getParentFile(),"crop" ));
Uri uri = Uri. fromFile(file);
Intent intent = getCropImageIntent(uri) ;
startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
}
/**
* 裁剪图片的方法
*
* @param uri
*/
public static Intent getCropImageIntent (Uri photoUri) {
Intent intentCarema = new Intent("com.android.camera.action.CROP" );
intentCarema.setDataAndType(photoUri, "image/*");
intentCarema.putExtra( "crop", true);
intentCarema.putExtra( "scale", true);
intentCarema.putExtra(MediaStore. EXTRA_OUTPUT, mCropUri);
intentCarema.putExtra( "outputFormat", Bitmap.CompressFormat.JPEG .toString());
//intentCarema.putExtra("FaceDetection", true);// 人脸识别功能
// intentCarema.putExtra("circleCrop", "");//设定此方法选定区域会是圆形区域
// aspectX aspectY是宽高比例
intentCarema.putExtra( "aspectX",5);
intentCarema.putExtra( "aspectY",4);
// outputX outputY 是裁剪图片的宽高
/* intentCarema.putExtra("outputX", 200);
intentCarema.putExtra("outputY", 300);*/
intentCarema.putExtra( "return-data", false );
return intentCarema;
}
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PHOTO_PICKED_WITH_DATA:
Bitmap bitmap = decodeUriAsBitmap( mCropUri);//decode bitmap
if (bitmap != null) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
try {
wallpaperManager.setBitmap(bitmap);
Toast. makeText(this, "壁纸设置成功", Toast. LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Android 图片裁剪
最新推荐文章于 2024-08-09 07:39:19 发布