我实现了一段从用户单元库中获取图片的代码,但我希望用户能够使用Android默认裁剪UI裁剪图像,所以我使用下面的代码:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 280);
intent.putExtra("outputY", 280);
intent.putExtra("scale", true);
startActivityForResult(intent , RESULT_CODE_PICK_FROM_LIBRARY);
并且要恢复图像我正在使用onActivityResult上的代码:
Uri selectedImage = data.getData();
String tempPath = getPath(selectedImage);
Bitmap pickedImage = BitmapFactory.decodeFile(tempPath);
的getPath():
private String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA }; // MediaColumns.DATA // MediaStore.Images.Media.DATA
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
return filePath;
} else {
return null;
}
}
但是我在行上得到一个空指针异常:
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
有没有人对此有任何提示? 此外,代码将拍摄一张照片,呈现裁剪选项然后检索简单图像......没有必要使用原始文件..
谢谢, 牛顿
答案 0 :(得分:1)
这Tutorial正是您所需要的:
意图选择操作后裁剪图像
答案 1 :(得分:1)
使用此功能裁剪位图
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if(bmp.getWidth() != radius || bmp.getHeight() != radius)
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
else
sbmp = bmp;
Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),
sbmp.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffa19774;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
// canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
// sbmp.getWidth() / 2+0.1f, paint);
canvas.drawCircle(sbmp.getWidth()/2, sbmp.getHeight()/2,
(sbmp.getWidth()/2), paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}