【发布时间】:2015-07-08 17:58:33
【问题描述】:
我正在尝试裁剪从相机拍摄的图像并将其存储到 imageview - 我不确定如何定位我裁剪的图像,它似乎存储在画廊中,无论是否存在我都不会打扰。
问题: 我的裁剪方法有效地将原始图像放在我的图像视图中。
如何将裁剪后的图像分配给 imageview?
private static final int PICK_IMAGE = 0;
private static final int PICK_IMAGE_FROM_GALLERY = 1;
private static final int CROP_IMAGE = 2;
private Uri uri;
。 . .
btnPhotoCamera.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent camera=new Intent();
camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//camera.putExtra("crop", "true");
File f=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
uri = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myFile.jpg"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(camera, PICK_IMAGE);
}
});
然后是我的裁剪方法
private void performCrop(Uri picUri)
{
//NEXUS 5 OS 5 is example of this branch
// take care of exceptions
try {
// call the standard crop action intent (the user device may not
// support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(uri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// // indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// // // indicate output X and Y
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_IMAGE);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe)
{//NOT TESTED AS HW DOES NOT GO HERE
Toast toast = Toast.makeText(this,"This device doesn't support the crop action! Exception: " + anfe.toString(),Toast.LENGTH_SHORT);
toast.show();
}
}
然后是我的方法来捕获触发的意图(其他应用程序)并获取它们的结果。
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
if (resultCode==RESULT_OK )
{
if(requestCode == PICK_IMAGE) //reply from camera
{
performCrop(uri); //crop the picture
}
if(requestCode == CROP_IMAGE) //reply from crop
{
Bitmap bmp = getBitmap(uri);
imgView.setImageBitmap(bmp);
}
}
}
最后是我将 uri 转换为位图的方法 - 这就是我出错的地方,为什么这个 uri 没有被裁剪?我怎么能用他裁剪的覆盖这个uri?
private Bitmap getBitmap(Uri bitmap_uri) {
InputStream is=null;
try
{
is = this.getContentResolver().openInputStream(bitmap_uri);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return BitmapFactory.decodeStream(is);
}
}
【问题讨论】:
-
android:scaletype="fitCenter" 在你的 imageview 的 xml 中添加这个属性。它总是将其裁剪到中心
-
没有裁剪作品 - 我在我的画廊中看到裁剪的 img。为什么我的 imageview 显示的是原始照片?
-
看看this的答案。
-
您将原始图像 uri 设置为您的 ImageView pic。您需要使用 Intent 数据来设置裁剪图像。
-
我想你已经得到了答案。 :-)