我有一个选择图像的按钮,该图像显示所选图像的预览,一切正常,除了如果用户在发送图片之前再次单击,我希望刷新预览。我不确定我必须显示哪些代码行(不了解问题出在哪里),所以请不要犹豫。
单击发送按钮时,我不重置图片,但它可以工作。 我第一次使用imageButton,第二次使用imageView可单击(两个onClick代码相似)。 (奖金问题用户可以选择jpg和png来使用,但我不确定我的代码是否合适)。
预先感谢您的关注和帮助。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE || requestCode == REQUEST_GALLERY_IMAGE) {
if (resultCode == RESULT_OK) {
selectedImageUri = data.getData();
if (selectedImageUri != null) {
UCrop.Options options = new UCrop.Options();
options.setCompressionFormat(Bitmap.CompressFormat.PNG);
options.setCompressionQuality(100);
options.setShowCropGrid(true);
UCrop.of(selectedImageUri, Uri.fromFile(new File(getCacheDir(), "pic.png")))
.withAspectRatio(1, 1)
.withOptions(options)
.start(this);
}
}
}
if (requestCode == UCrop.REQUEST_CROP) {
if (resultCode == RESULT_OK) {
progresspicbar.setVisibility(View.VISIBLE);
selectedImageUri = UCrop.getOutput(data);
try {
File compressedFile = new Compressor(this).setCompressFormat(Bitmap.CompressFormat.PNG).setQuality(50).setMaxHeight(96).setMaxWidth(96).compressToFile(new File(selectedImageUri.getPath()));
mPhotoPreview.setVisibility(View.VISIBLE);
mPhotoPreview.setImageURI(Uri.fromFile(compressedFile));
mPhotoPickerButton.setVisibility(View.GONE);
storageReference.putFile(selectedImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
mPhotoUrl = uri.toString();
progresspicbar.setVisibility(GONE);
mSendButton.setEnabled(true);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progresspicbar.setVisibility(GONE);
if (mPhotoUrl.isEmpty()) {
if (mPhotoPreview.getVisibility() == View.VISIBLE) {
mSendButton.setEnabled(false);
mPhotoPreview.setVisibility(GONE);
mPhotoPickerButton.setVisibility(View.VISIBLE);
}
}
}
});
} else if (mPhotoUrl.isEmpty()) {
progresspicbar.setVisibility(GONE);
if (mPhotoPreview.getVisibility() == View.VISIBLE) {
mSendButton.setEnabled(false);
mPhotoPreview.setVisibility(GONE);
mPhotoPickerButton.setVisibility(View.VISIBLE);
}
}
}
});
} catch (IOException e) {
progresspicbar.setVisibility(GONE);
e.printStackTrace();
}
} else if (resultCode == UCrop.RESULT_ERROR) {
progresspicbar.setVisibility(GONE);
Log.e("Error", "Crop error:" + Objects.requireNonNull(UCrop.getError(data)).getMessage());
}
}
}