Last active
October 26, 2018 19:36
-
-
Save gxlinda/279e012e2d424991450dd508a7fc9477 to your computer and use it in GitHub Desktop.
Local image saving to, and retrieving from Firebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Modified code for 'Make an Android App like WhatsApp' YouTube tutorial series | |
//(https://www.youtube.com/playlist?list=PLxefhmF0pcPmtdoud8f64EpgapkclCllj) | |
//to work with Firebase dependency firebase-database:16.0.4 | |
//as task.getResult().getDownloadUrl().toString() works only with earlier versions | |
// | |
//... | |
// | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { | |
CropImage.ActivityResult result = CropImage.getActivityResult(data); | |
if (resultCode == RESULT_OK) { | |
loadingBar.setTitle("Set profile image"); | |
loadingBar.setMessage("Please wait until your profile image is updating"); | |
loadingBar.setCanceledOnTouchOutside(false); | |
loadingBar.show(); | |
Uri resultUri = result.getUri(); | |
final StorageReference imagePathRef = userProfileImageReference.child(currentUserId + ".jpg"); | |
final UploadTask uploadTask = imagePathRef.putFile(resultUri); | |
// Register observers to listen for when the download is done or if it fails | |
uploadTask.addOnFailureListener(new OnFailureListener() { | |
@Override | |
public void onFailure(@NonNull Exception exception) { | |
String message = exception.toString(); | |
Toast.makeText(SettingsActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show(); | |
loadingBar.dismiss(); | |
} | |
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { | |
@Override | |
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { | |
Toast.makeText(SettingsActivity.this, "Profile image uploaded successfully", Toast.LENGTH_SHORT).show(); | |
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() { | |
@Override | |
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception { | |
if (!task.isSuccessful()) { | |
throw task.getException(); | |
} | |
// Continue with the task to get the download URL | |
String imageUrl = imagePathRef.getDownloadUrl().toString(); //for debugging | |
return imagePathRef.getDownloadUrl(); | |
} | |
}).addOnCompleteListener(new OnCompleteListener<Uri>() { | |
@Override | |
public void onComplete(@NonNull Task<Uri> task) { | |
if (task.isSuccessful()) { | |
String downloadUrl = task.getResult().toString(); | |
rootReference.child("Users").child(currentUserId).child("image") | |
.setValue(downloadUrl) | |
.addOnCompleteListener(new OnCompleteListener<Void>() { | |
@Override | |
public void onComplete(@NonNull Task<Void> task) { | |
if (task.isSuccessful()) { | |
Toast.makeText(SettingsActivity.this, "Profile image saved to database successfully", Toast.LENGTH_SHORT).show(); | |
loadingBar.dismiss(); | |
} else { | |
String message = task.getException().toString(); | |
Toast.makeText(SettingsActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show(); | |
loadingBar.dismiss(); | |
} | |
} | |
}); | |
} else { | |
String message = task.getException().toString(); | |
Toast.makeText(SettingsActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show(); | |
loadingBar.dismiss(); | |
} | |
} | |
}); | |
} | |
}); | |
} | |
} | |
} | |
// | |
//... | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment