在我的应用程序个人资料活动中,我希望允许用户上传两种类型的图片:个人资料图片和封面图片,但由于我不知道采用正确的方法,因此我陷入了困境。我已经成功完成了用户个人资料图片的代码,现在我面临的唯一问题是封面图片。
下面是从设备中选择封面图像的代码
new AlertDialog.Builder(SettingsActivity.this)
.setTitle("Change Cover")
// .setMessage("Change Cover")
.setPositiveButton("CHANGE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent, "SELECT IMAGE"), GALLERY_PICKER);
}
})
.show();
这是用于个人资料图片的
mImagebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent, "SELECT IMAGE"), GALLERY_PICK);
}
});
这是处理两个任务的代码
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICK && resultCode == RESULT_OK) {
mPdialog = new ProgressDialog(SettingsActivity.this);
mPdialog.setTitle("Uploading");
mPdialog.setMessage("please wait....");
mPdialog.show();
Uri imageUri = data.getData();
CropImage.activity(imageUri)
.setAspectRatio(1, 1)
.setMinCropWindowSize(500,500)
.start(SettingsActivity.this);
}
if(requestCode == GALLERY_PICKER && resultCode == RESULT_OK
&& data != null && data.getData() != null )
{
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
mProfileCover.setImageBitmap(bitmap);
}
catch (IOException e)
{
e.printStackTrace();
}
File file_thumb = new File(filePath.getPath());
Bitmap thumb_bitmap = new Compressor(this)
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(file_thumb);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
final byte[] thumb_byte = baos.toByteArray();
String current_uid = mCurrentuser.getUid();
StorageReference filepath = mImageReference.child("profile_cover").child(current_uid + ".jpg");
final StorageReference thumb_path = mImageReference.child("profile_cover").child("thumbs").child(current_uid + ".jpg");
filepath.putFile(filePath).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()){
final String download_url = task.getResult().getDownloadUrl().toString();
UploadTask uploadTask = thumb_path.putBytes(thumb_byte);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> thumb_task) {
String thumb_downloadurl = thumb_task.getResult().getDownloadUrl().toString();
if (thumb_task.isSuccessful()){
Map update_hashmap = new HashMap();
update_hashmap.put("cover_image",download_url);
update_hashmap.put("cover_thumb", thumb_downloadurl);
mDatabase.updateChildren(update_hashmap).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()){
mPdialog.dismiss();
Toast.makeText(SettingsActivity.this,"success",Toast.LENGTH_LONG).show();
}
}
});
}else {
Toast.makeText(SettingsActivity.this,"failed",Toast.LENGTH_LONG).show();
mPdialog.dismiss();
}
}
});
}
}
});
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
File file_thumb = new File(resultUri.getPath());
String current_uid = mCurrentuser.getUid();
Bitmap thumb_bitmap = new Compressor(this)
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(file_thumb);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
final byte[] thumb_byte = baos.toByteArray();
//the rest of the profile image task
但是我现在很困惑,因为我在想这个
startActivityForResult(Intent.createChooser(galleryIntent, "SELECT IMAGE"), GALLERY_PICK);
和
startActivityForResult(Intent.createChooser(galleryIntent, "SELECT IMAGE"), GALLERY_PICKER);
应该区分任务,但不能完全那样工作。上传图片时,将调用CropImage
,尽管按预期在数据库中创建了文件夹,但上传的图像也会替换当前的个人资料图像。
请问什么是正确处理两种任务的正确方法