Skip to content

Instantly share code, notes, and snippets.

@msomu
Created April 4, 2018 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msomu/5a5d1dc51ec536d3c9746986154bc9cd to your computer and use it in GitHub Desktop.
Save msomu/5a5d1dc51ec536d3c9746986154bc9cd to your computer and use it in GitHub Desktop.
Add Image to Firebase Storage
dependencies {
compile 'com.google.firebase:firebase-storage:12.0.1'
}
public class MainActivity extends AppCompatActivity {
FirebaseStorage storage = FirebaseStorage.getInstance();
// Fetch the Image file uri and replace it with 'new File("path/to/images/rivers.jpg")'
Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
uploadTask = riversRef.putFile(file);
// 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) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment