Android 让用户裁剪图像
我很难想出如何让用户裁剪图片。 我想给位图变量加载位图裁剪图片之前设置为壁纸。但我没能做到。。。这是我试过的 第一版正常工作,但返回的图像分辨率较差。将输出更改为更高的值没有帮助。正如我在一些帖子中看到的,不建议使用摄像头,因为并非所有设备都支持这一点Android 让用户裁剪图像,android,android-intent,crop,Android,Android Intent,Crop,我很难想出如何让用户裁剪图片。 我想给位图变量加载位图裁剪图片之前设置为壁纸。但我没能做到。。。这是我试过的 第一版正常工作,但返回的图像分辨率较差。将输出更改为更高的值没有帮助。正如我在一些帖子中看到的,不建议使用摄像头,因为并非所有设备都支持这一点 Intent intent = new Intent("com.android.camera.action.CROP"); String path = Images.Media.insertImage(context.getContentRe
Intent intent = new Intent("com.android.camera.action.CROP");
String path = Images.Media.insertImage(context.getContentResolver(), loaded,null, null);
Uri uri = Uri.parse(path);
intent.setData(uri);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, 2);
第二。加载图像选择器,然后进行裁剪。我如何配置它以直接在图像上加载裁剪?就像第1版一样
Intent photoPickerIntent = new Intent(MediaStore.ACTION_PICK);
photoPickerIntent.setData(uri);
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent, 2);
和非活动结果
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) { return; }
if(requestCode == 2) {
Bundle extras = data.getExtras();
if(extras != null ) {
Bitmap photo = extras.getParcelable("data");
loaded = photo;
}
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(loaded);
} catch (IOException e) {}
}
}
我不知道这些方法是否正确,但我希望有人能给我指出正确的方向。其中,为什么,以及如何使用
更新:我仍在等待有人指出如何正确操作,下面的答案有效,但返回的图像分辨率较差,因此它们不是使用的选项我在使用相机和
操作时也遇到了问题,返回的图像非常小,尽管通过的分辨率要大得多。我绕过了这个问题,将生成的裁剪图像存储在一个临时文件中
// temporary storage location, preferably in your cache directory
private final String tempFilePath = "somePath";
// URI instantiated with your temporary storage location
private Uri tempuri = Uri.fromFile(new File(tempFilePath));
// code for startActivityForResult
private final static int ACTIVITY_CROP_IMAGE = 1;
像这样称呼意图
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra("aspectX", wallpaperWidth);
photoPickerIntent.putExtra("aspectY", wallpaperHeight);
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempuri);
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent, ACTIVITY_CROP_IMAGE);
然后在onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK)
return;
if(requestCode == ACTIVITY_CROP_IMAGE)
{
try
{
Bitmap bmp = BitmapFactory.decodeFile(tempFilePath);
if(bmp != null)
{
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setBitmap(bmp);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(tempFilePath != null)
{
File tempFile = new File(tempFilePath);
if(tempFile.exists())
{
tempFile.delete();
}
}
}
}
}
我从头开始构建了上面的代码,实际上并没有编译任何代码。但是基础是正确的,你应该掌握窍门;-) 首先,变量:
final int PIC_CROP = 2;
Uri imageUri;
Bitmap thePic;
在从相机或多媒体资料中拍摄照片之前,请将图像放入Uri(imageUri),在try/catch中使用一种称为“performCrop()”的方法:
private void performCrop(){
try {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
if (size >= 0) {
intent.setData(imageUri);
intent.putExtra("crop", "false");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, PIC_CROP);
}
}
catch(ActivityNotFoundException anfe){
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
好的,亲爱的,我把我的作物图像的全部代码放在Android上了。
这是全局变量
//This For Image Crop
/**
* Uri for set image crop option .
*/
private Uri mImageCaptureUri;
/**
* int for set key and get key from result activity .
*/
public final int CROP_FROM_CAMERA = 0;
/**
* Bitmap for apply Crop Operation Result.
*/
private Bitmap _tempOpration;
//This is Crop Method.
/**
* Method for apply Crop .
* @param filePath - String path of file .
*/
private void doCrop(String filePath){
try{
//New Flow
mImageCaptureUri = Uri.fromFile(new File(filePath));
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
if (size == 0)
{
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return;
}
else
{
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (size == 1)
{
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
}
else
{
for (ResolveInfo res : list)
{
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
co.appIntent= new Intent(intent);
co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter( adapter, new DialogInterface.OnClickListener()
{
public void onClick( DialogInterface dialog, int item )
{
startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
}
});
builder.setOnCancelListener( new DialogInterface.OnCancelListener()
{
public void onCancel( DialogInterface dialog )
{
if (mImageCaptureUri != null )
{
getContentResolver().delete(mImageCaptureUri, null, null );
mImageCaptureUri = null;
}
}
} );
AlertDialog alert = builder.create();
alert.show();
}
}
}
catch (Exception ex)
{
genHelper.showErrorLog("Error in Crop Function-->"+ex.toString());
}
}
这用于显示列表
CropOptionAdapter
public class CropOptionAdapter extends ArrayAdapter<CropOption>
{
private ArrayList<CropOption> mOptions;
private LayoutInflater mInflater;
public CropOptionAdapter(Context context, ArrayList<CropOption> options)
{
super(context, R.layout.crop_selector, options);
mOptions = options;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup group)
{
if (convertView == null)
convertView = mInflater.inflate(R.layout.crop_selector, null);
CropOption item = mOptions.get(position);
if (item != null) {
((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon);
((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title);
return convertView;
}
return null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
//在我的live应用程序上使用这种类型
genHelper.showtoos(“此区域无作物活动”)强>
是帮助显示toast消息和错误日志的常规类
祝你好运。正如类似帖子中提到的,Android没有官方的裁剪意图
(),所以我不会使用“com.android.camera.action.CROP”
然而,自从这个问题最初发布以来,Android在Kitkat(19级)中添加了一个新的API,允许用户调用裁剪图像并设置为壁纸活动。请参阅WallperManager.getCropandSetWallperIntent(),这可能会解决您最初的问题。在用户返回到您的活动之前,只需添加裁剪方法即可。在你设置view.setImageBitmap或任何你正在做的事情通过裁剪方法之前。@Datenshi我会带着你的解决方案回来使用这个链接,这是一个可以解决你问题的演示项目代码。@Datenshi只要试试这段代码,我知道这次我没有错!!非常感谢。我会试试这个然后我有空闲时间你开始加载作物活动,加载失败:\@Datenshi亲爱的只需使用我的代码即可在我的live app中正常工作没有任何问题。亲爱的,我给你我的完整代码。在说了之后就执行。另外,如果没有找到任何作物,我会管理这件事。只需检查然后执行,然后查看。我知道这可能是我的错误用法。我正在调查。有一个问题,您将什么类型的uri传递给doCrop函数?当前我尝试传递web url,或者图像应该缓存到手机中亲爱的,请稍等几分钟我回答如果您的图像存储在图片文件夹中的sdcard中,请给出此类型的路径它的工作已完成请尝试此操作。文件路径===>/mnt/sdcard/Pictures/test02.png如何将drawable作为imageuri传递?
public class CropOptionAdapter extends ArrayAdapter<CropOption>
{
private ArrayList<CropOption> mOptions;
private LayoutInflater mInflater;
public CropOptionAdapter(Context context, ArrayList<CropOption> options)
{
super(context, R.layout.crop_selector, options);
mOptions = options;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup group)
{
if (convertView == null)
convertView = mInflater.inflate(R.layout.crop_selector, null);
CropOption item = mOptions.get(position);
if (item != null) {
((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon);
((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title);
return convertView;
}
return null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
/**
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) return;
switch (requestCode)
{
case CROP_FROM_CAMERA:
if (data == null)
{
genHelper.showToast("No Crop Activity in This");
return;
}
final Bundle extras = data.getExtras();
if (extras != null)
{
try
{
_tempOpration=extras.getParcelable("data");
imageLayout.setImageBitmap(_tempOpration);
_tempOpration=null;
}
catch (Exception e)
{
e.printStackTrace();
}
}
break;
}
}