Android 来自裁剪意图的完整图像
Android Full Image From Crop Intent
我正在使用 Android phone 上的相机拍照。拍摄图像后,我将完整图像作为 uri 传递给裁剪意图 (com.android.camera.action.CROP)。但是,当裁剪意图返回时,它 returns 在缩略图大小的可打包文件中。我试图将来自相机的图像保存在文件中并发送文件的 uri,但它不会在裁剪意图中打开图像。如何从它发回的包裹中获取我在裁剪意图中裁剪的全尺寸图像?这是我正在使用的一些代码:
public void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (resultCode != Activity.RESULT_CANCELED) {
Uri uri = null;
switch (requestCode) {
case FILE_SELECT_CODE:
if (intent.getData() != null) {
uri = intent.getData();
cropSelectedPicture(uri);
}
break;
case PICTURE_SELECT_CODE:
uri = intent.getData();
if (uri == null) {
Bundle bundle = intent.getExtras();
Bitmap photo = null;
if (bundle != null) {
photo = bundle.getParcelable("data");
}
finishChangeProfilePicture(photo);
} else {
cropSelectedPicture(uri);
}
break;
case FILE_CROP_CODE:
Bundle extras = intent.getExtras();
Bitmap thePic = extras.getParcelable("data");
uri = extras.getParcelable("uri");
finishChangeProfilePicture(thePic);
break;
}
}
super.onActivityResult(requestCode, resultCode, intent);
}
public void showCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("crop", "true");
intent.putExtra("outputX", pictureWidth);
intent.putExtra("outputY", pictureHeight);
intent.putExtra("aspectX", pictureWidth);
intent.putExtra("aspectY", pictureHeight);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
try {
cordova.startActivityForResult(
this, Intent.createChooser(intent, "Take Picture"),
PICTURE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(context, "Please install a Camera application.",
Toast.LENGTH_SHORT).show();
}
}
public void cropSelectedPicture(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("outputX", pictureWidth);
intent.putExtra("outputY", pictureHeight);
intent.putExtra("aspectX", pictureWidth);
intent.putExtra("aspectY", pictureHeight);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
for (int i = 0; i < activities.size(); i++) {
String label = (String) activities.get(i).loadLabel(packageManager);
if (label.equals("Crop picture")) {
ActivityInfo activity = activities.get(i).activityInfo;
ComponentName name = new ComponentName (activity.applicationInfo.packageName,
activity.name);
intent.setComponent(name);
}
}
}
try {
cordova.startActivityForResult(this, intent, FILE_CROP_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Please install a Cropping program", Toast.LENGTH_SHORT).show();
}
}
好的,所以我要做的是从相机意图中保存 uri,然后在从裁剪意图中出来时将图像保存到该意图。我之前曾尝试过一次,但它是应用程序的内部文件而不是内容解析器。 (公平警告:这是一个 cordova 项目,而不是原生 android 项目,所以如果您在 Media.getBitmap 行上遇到问题,请删除 cordova.getActivity() 部分,这应该它有效)这是我改变的:
public void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (resultCode != Activity.RESULT_CANCELED) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (intent.getData() != null) {
uri = intent.getData();
cropSelectedPicture();
}
break;
case PICTURE_SELECT_CODE:
uri = intent.getData();
if (uri == null) {
Bundle bundle = intent.getExtras();
Bitmap photo = null;
if (bundle != null) {
photo = bundle.getParcelable("data");
}
finishChangeProfilePicture(photo);
} else {
cropSelectedPicture();
}
break;
case FILE_CROP_CODE:
Bitmap theUriPic;
try {
theUriPic = Media.getBitmap(cordova.getActivity().getContentResolver(), uri);
finishChangeProfilePicture(theUriPic);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
super.onActivityResult(requestCode, resultCode, intent);
}
public void cropSelectedPicture() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("outputX", pictureWidth);
intent.putExtra("outputY", pictureHeight);
intent.putExtra("aspectX", pictureWidth);
intent.putExtra("aspectY", pictureHeight);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra("return-data", true);
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
for (int i = 0; i < activities.size(); i++) {
String label = (String) activities.get(i).loadLabel(packageManager);
if (label.equals("Crop picture")) {
ActivityInfo activity = activities.get(i).activityInfo;
ComponentName name = new ComponentName (activity.applicationInfo.packageName,
activity.name);
intent.setComponent(name);
}
}
}
try {
cordova.startActivityForResult(this, intent, FILE_CROP_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Please install a Cropping program", Toast.LENGTH_SHORT).show();
}
}
我正在使用 Android phone 上的相机拍照。拍摄图像后,我将完整图像作为 uri 传递给裁剪意图 (com.android.camera.action.CROP)。但是,当裁剪意图返回时,它 returns 在缩略图大小的可打包文件中。我试图将来自相机的图像保存在文件中并发送文件的 uri,但它不会在裁剪意图中打开图像。如何从它发回的包裹中获取我在裁剪意图中裁剪的全尺寸图像?这是我正在使用的一些代码:
public void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (resultCode != Activity.RESULT_CANCELED) {
Uri uri = null;
switch (requestCode) {
case FILE_SELECT_CODE:
if (intent.getData() != null) {
uri = intent.getData();
cropSelectedPicture(uri);
}
break;
case PICTURE_SELECT_CODE:
uri = intent.getData();
if (uri == null) {
Bundle bundle = intent.getExtras();
Bitmap photo = null;
if (bundle != null) {
photo = bundle.getParcelable("data");
}
finishChangeProfilePicture(photo);
} else {
cropSelectedPicture(uri);
}
break;
case FILE_CROP_CODE:
Bundle extras = intent.getExtras();
Bitmap thePic = extras.getParcelable("data");
uri = extras.getParcelable("uri");
finishChangeProfilePicture(thePic);
break;
}
}
super.onActivityResult(requestCode, resultCode, intent);
}
public void showCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("crop", "true");
intent.putExtra("outputX", pictureWidth);
intent.putExtra("outputY", pictureHeight);
intent.putExtra("aspectX", pictureWidth);
intent.putExtra("aspectY", pictureHeight);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
try {
cordova.startActivityForResult(
this, Intent.createChooser(intent, "Take Picture"),
PICTURE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(context, "Please install a Camera application.",
Toast.LENGTH_SHORT).show();
}
}
public void cropSelectedPicture(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("outputX", pictureWidth);
intent.putExtra("outputY", pictureHeight);
intent.putExtra("aspectX", pictureWidth);
intent.putExtra("aspectY", pictureHeight);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
for (int i = 0; i < activities.size(); i++) {
String label = (String) activities.get(i).loadLabel(packageManager);
if (label.equals("Crop picture")) {
ActivityInfo activity = activities.get(i).activityInfo;
ComponentName name = new ComponentName (activity.applicationInfo.packageName,
activity.name);
intent.setComponent(name);
}
}
}
try {
cordova.startActivityForResult(this, intent, FILE_CROP_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Please install a Cropping program", Toast.LENGTH_SHORT).show();
}
}
好的,所以我要做的是从相机意图中保存 uri,然后在从裁剪意图中出来时将图像保存到该意图。我之前曾尝试过一次,但它是应用程序的内部文件而不是内容解析器。 (公平警告:这是一个 cordova 项目,而不是原生 android 项目,所以如果您在 Media.getBitmap 行上遇到问题,请删除 cordova.getActivity() 部分,这应该它有效)这是我改变的:
public void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (resultCode != Activity.RESULT_CANCELED) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (intent.getData() != null) {
uri = intent.getData();
cropSelectedPicture();
}
break;
case PICTURE_SELECT_CODE:
uri = intent.getData();
if (uri == null) {
Bundle bundle = intent.getExtras();
Bitmap photo = null;
if (bundle != null) {
photo = bundle.getParcelable("data");
}
finishChangeProfilePicture(photo);
} else {
cropSelectedPicture();
}
break;
case FILE_CROP_CODE:
Bitmap theUriPic;
try {
theUriPic = Media.getBitmap(cordova.getActivity().getContentResolver(), uri);
finishChangeProfilePicture(theUriPic);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
super.onActivityResult(requestCode, resultCode, intent);
}
public void cropSelectedPicture() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("outputX", pictureWidth);
intent.putExtra("outputY", pictureHeight);
intent.putExtra("aspectX", pictureWidth);
intent.putExtra("aspectY", pictureHeight);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra("return-data", true);
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
for (int i = 0; i < activities.size(); i++) {
String label = (String) activities.get(i).loadLabel(packageManager);
if (label.equals("Crop picture")) {
ActivityInfo activity = activities.get(i).activityInfo;
ComponentName name = new ComponentName (activity.applicationInfo.packageName,
activity.name);
intent.setComponent(name);
}
}
}
try {
cordova.startActivityForResult(this, intent, FILE_CROP_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Please install a Cropping program", Toast.LENGTH_SHORT).show();
}
}