Java Code Examples for com.google.firebase.storage.StorageReference#putBytes()

The following examples show how to use com.google.firebase.storage.StorageReference#putBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StorageTest.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void putShouldFailWithNotAuthorized() throws Exception {
  FirebaseAuth auth = FirebaseAuth.getInstance();
  FirebaseStorage storage = FirebaseStorage.getInstance();

  auth.signOut();
  StorageReference blob = storage.getReference("restaurants").child(TestId.create());
  byte[] data = "Google NYC".getBytes(StandardCharsets.UTF_8);

  try {
    Task<?> putTask = blob.putBytes(Arrays.copyOf(data, data.length));
    Throwable failure = Tasks2.waitForFailure(putTask);
    StorageException ex = (StorageException) failure;
    assertThat(ex.getErrorCode()).isEqualTo(StorageException.ERROR_NOT_AUTHORIZED);
  } finally {
    Tasks2.waitBestEffort(blob.delete());
  }
}
 
Example 2
Source File: StorageTest.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void getShouldReturnNewlyPutData() throws Exception {
  FirebaseAuth auth = FirebaseAuth.getInstance();
  FirebaseStorage storage = FirebaseStorage.getInstance();

  auth.signOut();
  Task<?> signInTask = auth.signInWithEmailAndPassword("[email protected]", "password");
  Tasks2.waitForSuccess(signInTask);

  StorageReference blob = storage.getReference("restaurants").child(TestId.create());

  byte[] data = "Google NYC".getBytes(StandardCharsets.UTF_8);

  try {
    Task<?> putTask = blob.putBytes(Arrays.copyOf(data, data.length));
    Tasks2.waitForSuccess(putTask);

    Task<byte[]> getTask = blob.getBytes(128);
    Tasks2.waitForSuccess(getTask);

    byte[] result = getTask.getResult();
    assertThat(result).isEqualTo(data);
  } finally {
    Tasks2.waitBestEffort(blob.delete());
  }
}
 
Example 3
Source File: DoorbellActivity.java    From doorbell with Apache License 2.0 5 votes vote down vote up
/**
 * Upload image data to Firebase as a doorbell event.
 */
private void onPictureTaken(final byte[] imageBytes) {
    if (imageBytes != null) {
        final DatabaseReference log = mDatabase.getReference("logs").push();
        final StorageReference imageRef = mStorage.getReference().child(log.getKey());

        // upload image to storage
        UploadTask task = imageRef.putBytes(imageBytes);
        task.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Uri downloadUrl = taskSnapshot.getDownloadUrl();
                // mark image in the database
                Log.i(TAG, "Image upload successful");
                log.child("timestamp").setValue(ServerValue.TIMESTAMP);
                log.child("image").setValue(downloadUrl.toString());
                // process image annotations
                annotateImage(log, imageBytes);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // clean up this entry
                Log.w(TAG, "Unable to upload image to Firebase");
                log.removeValue();
            }
        });
    }
}
 
Example 4
Source File: UploadProcessor.java    From gdx-fireapp with Apache License 2.0 4 votes vote down vote up
void processUpload(final FirebaseStorage firebaseStorage, final String path, final byte[] data, final FuturePromise<FileMetadata> promise) {
    StorageReference dataRef = firebaseStorage.getReference().child(path);
    UploadTask uploadTask = dataRef.putBytes(data);
    processUpload(firebaseStorage, uploadTask, promise);
}