Skip to content

Instantly share code, notes, and snippets.

@mogeta
Created September 15, 2016 15:40
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 mogeta/d8abda24799d407673af3c7f908e927f to your computer and use it in GitHub Desktop.
Save mogeta/d8abda24799d407673af3c7f908e927f to your computer and use it in GitHub Desktop.
//upload image for firebase storage
private void updateImage(ImageView imageView){
// Create a storage reference from our app
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("<your-storage-url>");
String uuid = UUID.randomUUID().toString();
StorageReference mountainsRef = storageRef.child(uuid + ".jpg");
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = mountainsRef.putBytes(data);
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();
AddNewImageURL(downloadUrl);
Log.e("info", downloadUrl.toString());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment