Skip to content

Instantly share code, notes, and snippets.

@Feroz-Istar
Created June 10, 2020 21:12
Show Gist options
  • Save Feroz-Istar/c7920591c3cbff6270a1978f9567f1de to your computer and use it in GitHub Desktop.
Save Feroz-Istar/c7920591c3cbff6270a1978f9567f1de to your computer and use it in GitHub Desktop.
FirebaseStorage storage = FirebaseStorage.getInstance("gs://istar-user-images");
StorageMetadata metadata = new StorageMetadata.Builder()
.setContentType(mimetype)
.build();
StorageReference storageRef = storage.getReference();
String file_name = UUID.randomUUID().toString();
Log.d(TAG,"file name "+file_name);
StorageReference imagesRef = storageRef.child("files/"+file_name);
UploadTask uploadTask = imagesRef.putFile(Uri.fromFile(local_profile.pathFile()),metadata);
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
return imagesRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
String link = "https://storage.googleapis.com/istar-user-images/files/"+file_name;
Log.d(TAG,"Uploaded link "+link);
hideProgressBar();
} else {
hideProgressBar();
showToast("Error Uploading Image");
// Handle failures
// ...
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment