这是我的用于将个人资料图片更新为firebase的代码,但它一直显示出问题,并且永远不会更新个人资料图片,因为从外部存储设备或相机中获取图片后,它会更新
private void uploadImage(){
final ProgressDialog progressDialog =new ProgressDialog(this);
progressDialog.setMessage("Uploading");
progressDialog.show();
if (mImageUri!=null){
final StorageReference filereference=storageReference.child(System.currentTimeMillis()+"."+getFileExtension(mImageUri));
uploadTask=filereference.putFile(mImageUri);
uploadTask.continueWithTask((Continuation) task -> {
if (!task.isSuccessful()){
throw task.getException();
}
return filereference.getDownloadUrl();
}).addOnCompleteListener((OnCompleteListener<Uri>) task -> {
if (task.isSuccessful()){
Uri downloadUri=task.getResult();
String myurl=downloadUri.toString();
DatabaseReference reference=FirebaseDatabase.getInstance().getReference("users").child(firebaseUser.getUid());
HashMap<String,Object> hashMap=new HashMap<>();
hashMap.put("imageurl",""+myurl);
reference.updateChildren(hashMap);
progressDialog.dismiss();
}else {
snackbar= Snackbar.make(view," failed",Snackbar.LENGTH_LONG);
setSnackbar();
}
}).addOnFailureListener(e -> {
snackbar= Snackbar.make(view,e.getMessage(),Snackbar.LENGTH_LONG);
setSnackbar();
});
}
else {
snackbar= Snackbar.make(view,"no Image selected",Snackbar.LENGTH_LONG);
setSnackbar();
}
}
这是我要求用户的外部存储权限
的方式 public boolean checkPermissionREAD_EXTERNAL_STORAGE(
final Context context) {
int currentAPIVersion = Build.VERSION.SDK_INT;
if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(context,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((EditProfileActivity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
showDialog("External storage", context,
Manifest.permission.READ_EXTERNAL_STORAGE);
} else {
ActivityCompat
.requestPermissions(
(EditProfileActivity) context,
new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
return false;
} else {
return true;
}
} else {
return true;
}
}
public void showDialog(final String msg, final Context context,
final String permission) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage(msg + " permission is necessary");
alertBuilder.setPositiveButton(android.R.string.yes,
(dialog, which) -> ActivityCompat.requestPermissions((EditProfileActivity) context,
new String[] { permission },
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE));
AlertDialog alert = alertBuilder.create();
alert.show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do your stuff
} else {
Toast.makeText(EditProfileActivity.this, "GET_ACCOUNTS Denied",
Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
}
}
这是“活动结果”的操作方式,这是它显示错误的地方。
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode==RESULT_OK ) {
// do your stuff..
CropImage.ActivityResult result=CropImage.getActivityResult(data);
mImageUri = result.getUri();
uploadImage();
}else {
snackbar= Snackbar.make(view,"Something gone wrong",Snackbar.LENGTH_LONG);
setSnackbar();
}
}