Skip to content

Instantly share code, notes, and snippets.

@jirawatee
Last active April 17, 2019 04:58
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 jirawatee/606ca9b2729f832b54cf2074c708a376 to your computer and use it in GitHub Desktop.
Save jirawatee/606ca9b2729f832b54cf2074c708a376 to your computer and use it in GitHub Desktop.
Firebase Storage - UploadFromDataInMemory.java
private void uploadFromDataInMemory() {
Helper.showDialog(this);
// Get the data from an ImageView as bytes
mImageView.setDrawingCacheEnabled(true);
mImageView.buildDrawingCache();
Bitmap bitmap = mImageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
mUploadTask = imageRef.putBytes(data);
mUploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Helper.dismissDialog();
mTextView.setText(String.format("Failure: %s", exception.getMessage()));
}
}).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.
Helper.dismissDialog();
imageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
mTextView.setText(uri.toString());
}
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment