裁剪后如何从图库中选择图像并保存在应用程序中?
How to pick an image from gallery and save within the app after cropping?
我是 Android 开发的新手,我尽力从图库中挑选一张图片并在裁剪后保存在应用程序中,但我失败了。请帮我解决这个问题。我试图混合使用不同的代码,但对我来说没有任何效果。
if (resultCode == RESULT_OK) {
//Uri photoUri = data.getData();
//if (photoUri != null) {
// photoPickerIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
CropImage.activity(android.net.Uri.parse(data.getDataString()))
.setAspectRatio(1,1)
.setFixAspectRatio(true)
.start(activity);
CropImage.ActivityResult result1 = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
iv.setImageURI(result1.getUri());
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result1.getError();
Log.d(TAG, "onActivityResult: " + error.getMessage());
}
//currentImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
//selectedImage.setImageBitmap(currentImage);
// }
首先在你的项目Gradle.built(app:odle)文件中添加一个依赖项
喜欢
dependencies {
implementaion 'com.theartofdev.edmodo:android-image-cropper:2.8.+'
}
然后在清单文件中添加以下两个权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在您的 class 中创建一个常量
私人静态最终 int REQUEST_FOR_GALLARY = 1;
你会需要这个。
之后,将以下代码放入您的按钮中,您可以在该按钮上单击以打开图库或您正在使用的任何内容。
Intent gallaryIntent = new Intent();
gallaryIntent.setAction(Intent.ACTION_GET_CONTENT);
gallaryIntent.setType("image/*");
startActivityForResult(gallaryIntent, REQUEST_FOR_GALLARY);
之后在Activity你的Activityclass结果上重写方法
喜欢
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_FOR_GALLARY && resultCode == RESULT_OK && data !=
null) {
Uri imageUri = data.getData();
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
//Save image wherever you want to save it
}
}
}
现在根据您的招聘更改您的代码并将图像保存到您想要存储的任何位置。
我是 Android 开发的新手,我尽力从图库中挑选一张图片并在裁剪后保存在应用程序中,但我失败了。请帮我解决这个问题。我试图混合使用不同的代码,但对我来说没有任何效果。
if (resultCode == RESULT_OK) {
//Uri photoUri = data.getData();
//if (photoUri != null) {
// photoPickerIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
CropImage.activity(android.net.Uri.parse(data.getDataString()))
.setAspectRatio(1,1)
.setFixAspectRatio(true)
.start(activity);
CropImage.ActivityResult result1 = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
iv.setImageURI(result1.getUri());
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result1.getError();
Log.d(TAG, "onActivityResult: " + error.getMessage());
}
//currentImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
//selectedImage.setImageBitmap(currentImage);
// }
首先在你的项目Gradle.built(app:odle)文件中添加一个依赖项
喜欢
dependencies {
implementaion 'com.theartofdev.edmodo:android-image-cropper:2.8.+'
}
然后在清单文件中添加以下两个权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在您的 class 中创建一个常量 私人静态最终 int REQUEST_FOR_GALLARY = 1; 你会需要这个。
之后,将以下代码放入您的按钮中,您可以在该按钮上单击以打开图库或您正在使用的任何内容。
Intent gallaryIntent = new Intent();
gallaryIntent.setAction(Intent.ACTION_GET_CONTENT);
gallaryIntent.setType("image/*");
startActivityForResult(gallaryIntent, REQUEST_FOR_GALLARY);
之后在Activity你的Activityclass结果上重写方法 喜欢
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_FOR_GALLARY && resultCode == RESULT_OK && data !=
null) {
Uri imageUri = data.getData();
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
//Save image wherever you want to save it
}
}
}
现在根据您的招聘更改您的代码并将图像保存到您想要存储的任何位置。