gpt4 book ai didi

android - 在 Android 4.4 中从图库中选择图像时裁剪图像

转载 作者:行者123 更新时间:2023-11-29 01:43:59 25 4
gpt4 key购买 nike

我正在尝试为从图库中选取的图像提供裁剪工具,它工作正常,但 Android 4.4 除外。如何解决这个问题?我正在使用以下代码

这就是我基于 Android 版本调用图库 Intent 的方式

ImageView ivGallery = (ImageView) pop.findViewById(R.id.ivGallery);
ivGallery.setOnClickListener(new OnClickListener()
{
@SuppressLint("InlinedApi")
@Override
public void onClick(View v)
{
if (Build.VERSION.SDK_INT < 19)
{
Intent intent = new Intent();
pop.dismiss();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("return-data", true);
intent.putExtra("aspectX", 300);
intent.putExtra("aspectY", 300);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);

startActivityForResult(intent, StaticMembers.galleryRequestCode); //1=gallery
}
else
{
picUri = ImageUtils.getTempUri();
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
pop.dismiss();
startActivityForResult(intent, StaticMembers.GALLERY_KITKAT_INTENT_CALLED);
}

}
});

这是我的onActivityResult

@SuppressLint("NewApi")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == StaticMembers.galleryRequestCode && resultCode == Activity.RESULT_OK)
{
Utils.deleteTempFolder();
Bundle extras = data.getExtras();
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
bmp = extras.getParcelable("data");
thumbBitmap = Bitmap.createScaledBitmap(bmp, 100, 100, true);
imgdp.setImageBitmap(bmp);
setConfirmPicDialog();
}
else if (requestCode == StaticMembers.GALLERY_KITKAT_INTENT_CALLED && resultCode == Activity.RESULT_OK)
{
Log.d("kitkat", "Inside onActivity result for kitkat");
picUri = data.getData();
performCrop(); //what is to be done here?



}



}

private void performCrop()
{
try
{
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("return-data", true);
cropIntent.putExtra("aspectX", 300);
cropIntent.putExtra("aspectY", 300);
cropIntent.putExtra("outputX", 300);
cropIntent.putExtra("outputY", 300);
startActivityForResult(cropIntent, StaticMembers.galleryRequestCode);
}
catch (ActivityNotFoundException anfe)
{
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(UserInfoActivity.this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}

为了在 Android 4.4 中有 crop intent,应该对上面的代码进行哪些更改

最佳答案

使用 Gallery 的裁剪功能不是一个好习惯。它在某些固件中缺失,因此在某些情况下应用程序甚至可能会崩溃。

相反,您可以使用此库 android-cropimage

关于android - 在 Android 4.4 中从图库中选择图像时裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22629763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com