com.google.firebase.storage.UploadTask Java Examples

The following examples show how to use com.google.firebase.storage.UploadTask. 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: RxFirebaseStorageTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void putStream() {

    TestSubscriber<UploadTask.TaskSnapshot> testSubscriber = new TestSubscriber<>();
    RxFirebaseStorage.putStream(mockStorageRef, stream)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(uploadSnapshot);

    verify(mockStorageRef).putStream(stream);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(uploadSnapshot));
    testSubscriber.assertNotCompleted();
    testSubscriber.unsubscribe();
}
 
Example #2
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutFileWithMetadata_notSuccessful() {
  mockNotSuccessfulResultForTask(mockUploadTask, new IllegalStateException());
  when(mockStorageReference.putFile(mockUri, mockStorageMetadata)).thenReturn(mockUploadTask);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putFile(mockStorageReference, mockUri, mockStorageMetadata).subscribe(obs);
  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertError(IllegalStateException.class);
  obs.assertNoValues();
}
 
Example #3
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutFileWithMetadata() {
  mockSuccessfulResultForTask(mockUploadTask, mockUploadTaskSnapshot);
  when(mockStorageReference.putFile(mockUri, mockStorageMetadata)).thenReturn(mockUploadTask);
  when(mockUploadTaskSnapshot.getBytesTransferred()).thenReturn(1000L);
  when(mockUploadTaskSnapshot.getTotalByteCount()).thenReturn(1000L);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putFile(mockStorageReference, mockUri, mockStorageMetadata).subscribe(obs);

  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertNoErrors();
  obs.assertComplete();
  obs.assertValue(new Predicate<UploadTask.TaskSnapshot>() {
    @Override public boolean test(UploadTask.TaskSnapshot taskSnapshot) throws Exception {
      return taskSnapshot.getBytesTransferred() == taskSnapshot.getTotalByteCount()
          && taskSnapshot.getTotalByteCount() == 1000L;
    }
  });
}
 
Example #4
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutFile_notSuccessful() {
  mockNotSuccessfulResultForTask(mockUploadTask, new IllegalStateException());
  when(mockStorageReference.putFile(mockUri)).thenReturn(mockUploadTask);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putFile(mockStorageReference, mockUri).subscribe(obs);
  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertError(IllegalStateException.class);
  obs.assertNoValues();
}
 
Example #5
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutFileWithMetadataAndUri_notSuccessful() {
  mockNotSuccessfulResultForTask(mockUploadTask, new IllegalStateException());
  when(mockStorageReference.putFile(mockUri, mockStorageMetadata, mockUri)).thenReturn(
      mockUploadTask);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putFile(mockStorageReference, mockUri, mockStorageMetadata, mockUri)
      .subscribe(obs);
  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertError(IllegalStateException.class);
  obs.assertNoValues();
}
 
Example #6
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutStream() {
  mockSuccessfulResultForTask(mockUploadTask, mockUploadTaskSnapshot);
  when(mockStorageReference.putStream(mockInputStream)).thenReturn(mockUploadTask);
  when(mockUploadTaskSnapshot.getBytesTransferred()).thenReturn(1000L);
  when(mockUploadTaskSnapshot.getTotalByteCount()).thenReturn(1000L);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putStream(mockStorageReference, mockInputStream).subscribe(obs);

  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertNoErrors();
  obs.assertComplete();
  obs.assertValue(new Predicate<UploadTask.TaskSnapshot>() {
    @Override public boolean test(UploadTask.TaskSnapshot taskSnapshot) throws Exception {
      return taskSnapshot.getBytesTransferred() == taskSnapshot.getTotalByteCount()
          && taskSnapshot.getTotalByteCount() == 1000L;
    }
  });
}
 
Example #7
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutStream_notSuccessful() {
  mockNotSuccessfulResultForTask(mockUploadTask, new IllegalStateException());
  when(mockStorageReference.putStream(mockInputStream)).thenReturn(mockUploadTask);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putStream(mockStorageReference, mockInputStream).subscribe(obs);
  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertError(IllegalStateException.class);
  obs.assertNoValues();
}
 
Example #8
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutStreamWithMetadata_notSuccessful() {
  mockNotSuccessfulResultForTask(mockUploadTask, new IllegalStateException());
  when(mockStorageReference.putStream(mockInputStream, mockStorageMetadata)).thenReturn(
      mockUploadTask);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putStream(mockStorageReference, mockInputStream, mockStorageMetadata)
      .subscribe(obs);
  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertError(IllegalStateException.class);
  obs.assertNoValues();
}
 
Example #9
Source File: ProfileInteractor.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void createOrUpdateProfileWithImage(final Profile profile, Uri imageUri, final OnProfileCreatedListener onProfileCreatedListener) {
    String imageTitle = ImageUtil.generateImageTitle(UploadImagePrefix.PROFILE, profile.getId());
    UploadTask uploadTask = databaseHelper.uploadImage(imageUri, imageTitle);

    if (uploadTask != null) {
        uploadTask.addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                Uri downloadUrl = task.getResult().getDownloadUrl();
                LogUtil.logDebug(TAG, "successful upload image, image url: " + String.valueOf(downloadUrl));

                profile.setPhotoUrl(downloadUrl.toString());
                createOrUpdateProfile(profile, onProfileCreatedListener);

            } else {
                onProfileCreatedListener.onProfileCreated(false);
                LogUtil.logDebug(TAG, "fail to upload image");
            }

        });
    } else {
        onProfileCreatedListener.onProfileCreated(false);
        LogUtil.logDebug(TAG, "fail to upload image");
    }
}
 
Example #10
Source File: MainActivity.java    From Shipr-Community-Android with GNU General Public License v3.0 6 votes vote down vote up
private void uploadImageToStorage(StorageReference photoRef, Uri selectedImageUri) {

        UploadTask uploadTask = photoRef.putFile(selectedImageUri);

        Task<Uri> urlTask = uploadTask.continueWithTask(task -> {
            if (!task.isSuccessful()) {
                throw Objects.requireNonNull(task.getException());
            }

            // Continue with the task to get the download URL
            return photoRef.getDownloadUrl();
        }).addOnCompleteListener(task -> {
            if (task.isSuccessful()) {

                downloadUri = task.getResult();
                updateProfilePic(downloadUri);
            }  // Handle failures
            // ...

        });

    }
 
Example #11
Source File: StorageTest.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
@Test
public void upload1() {
    // Given
    Storage storage = new Storage();
    byte[] data = new byte[]{0, 0, 0, 1, 1, 1};
    final UploadTask uploadTask = Mockito.mock(UploadTask.class);
    when(storageReference.putBytes(Mockito.any(byte[].class))).thenReturn(uploadTask);
    when(uploadTask.addOnFailureListener(Mockito.any(OnFailureListener.class))).thenReturn(uploadTask);
    when(uploadTask.addOnSuccessListener(Mockito.any(OnSuccessListener.class))).then(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) {
            ((OnSuccessListener) invocation.getArgument(0)).onSuccess(Mockito.mock(UploadTask.TaskSnapshot.class));
            return uploadTask;
        }
    });

    // When
    storage.upload("test", data).subscribe();

    // Then
    Mockito.verify(storageReference, VerificationModeFactory.times(1)).putBytes(Mockito.any(byte[].class));
    Mockito.verify(uploadTask, VerificationModeFactory.times(1)).addOnFailureListener(Mockito.any(OnFailureListener.class));
    Mockito.verify(uploadTask, VerificationModeFactory.times(1)).addOnSuccessListener(Mockito.any(OnSuccessListener.class));
}
 
Example #12
Source File: PostInteractor.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void createOrUpdatePostWithImage(Uri imageUri, final OnPostCreatedListener onPostCreatedListener, final Post post) {
    // Register observers to listen for when the download is done or if it fails
    DatabaseHelper databaseHelper = ApplicationHelper.getDatabaseHelper();
    if (post.getId() == null) {
        post.setId(generatePostId());
    }

    final String imageTitle = ImageUtil.generatePostImageTitle(post.getId());
    UploadTask uploadTask = databaseHelper.uploadImage(imageUri, imageTitle);

    if (uploadTask != null) {
        uploadTask.addOnFailureListener(exception -> {
            // Handle unsuccessful uploads
            onPostCreatedListener.onPostSaved(false);

        }).addOnSuccessListener(taskSnapshot -> {
            // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.

            post.setImageTitle(imageTitle);
            createOrUpdatePost(post);

            onPostCreatedListener.onPostSaved(true);
        });
    }
}
 
Example #13
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutBytesWithMetadata_notSuccessful() {
  mockNotSuccessfulResultForTask(mockUploadTask, new IllegalStateException());
  when(mockStorageReference.putBytes(new byte[] { 1, 2, 3 }, mockStorageMetadata)).thenReturn(
      mockUploadTask);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putBytes(mockStorageReference, new byte[] { 1, 2, 3 }, mockStorageMetadata)
      .subscribe(obs);
  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertError(IllegalStateException.class);
  obs.assertNoValues();
}
 
Example #14
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutBytes_notSuccessful() {
  mockNotSuccessfulResultForTask(mockUploadTask, new IllegalStateException());
  when(mockStorageReference.putBytes(new byte[] { 1, 2, 3 })).thenReturn(mockUploadTask);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putBytes(mockStorageReference, new byte[] { 1, 2, 3 }).subscribe(obs);
  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertError(IllegalStateException.class);
  obs.assertNoValues();
}
 
Example #15
Source File: RxFirebaseStorageTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void putFileMetadataAndUri() {

    TestSubscriber<UploadTask.TaskSnapshot> testSubscriber = new TestSubscriber<>();
    RxFirebaseStorage.putFile(mockStorageRef, uri, metadata, uri)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(uploadSnapshot);

    verify(mockStorageRef).putFile(uri, metadata, uri);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(uploadSnapshot));
    testSubscriber.assertNotCompleted();
    testSubscriber.unsubscribe();
}
 
Example #16
Source File: RxFirebaseStorageTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void putFileMetadata() {

    TestSubscriber<UploadTask.TaskSnapshot> testSubscriber = new TestSubscriber<>();
    RxFirebaseStorage.putFile(mockStorageRef, uri, metadata)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(uploadSnapshot);

    verify(mockStorageRef).putFile(uri, metadata);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(uploadSnapshot));
    testSubscriber.assertNotCompleted();
    testSubscriber.unsubscribe();
}
 
Example #17
Source File: RxFirebaseStorageTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void putFile() {

    TestSubscriber<UploadTask.TaskSnapshot> testSubscriber = new TestSubscriber<>();
    RxFirebaseStorage.putFile(mockStorageRef, uri)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(uploadSnapshot);

    verify(mockStorageRef).putFile(uri);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(uploadSnapshot));
    testSubscriber.assertNotCompleted();
    testSubscriber.unsubscribe();
}
 
Example #18
Source File: RxFirebaseStorageTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void putBytesMetadata() {

    TestSubscriber<UploadTask.TaskSnapshot> testSubscriber = new TestSubscriber<>();
    RxFirebaseStorage.putBytes(mockStorageRef, bytes, metadata)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(uploadSnapshot);

    verify(mockStorageRef).putBytes(bytes, metadata);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(uploadSnapshot));
    testSubscriber.assertNotCompleted();
    testSubscriber.unsubscribe();
}
 
Example #19
Source File: RxFirebaseStorageTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void putBytes() {

    TestSubscriber<UploadTask.TaskSnapshot> testSubscriber = new TestSubscriber<>();
    RxFirebaseStorage.putBytes(mockStorageRef, bytes)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(uploadSnapshot);

    verify(mockStorageRef).putBytes(bytes);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(uploadSnapshot));
    testSubscriber.assertNotCompleted();
    testSubscriber.unsubscribe();
}
 
Example #20
Source File: RxFirebaseStorageTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void putStreamMetadata() {

    TestSubscriber<UploadTask.TaskSnapshot> testSubscriber = new TestSubscriber<>();
    RxFirebaseStorage.putStream(mockStorageRef, stream, metadata)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(uploadSnapshot);

    verify(mockStorageRef).putStream(stream, metadata);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(uploadSnapshot));
    testSubscriber.assertNotCompleted();
    testSubscriber.unsubscribe();
}
 
Example #21
Source File: FirestackStorage.java    From react-native-firestack with MIT License 6 votes vote down vote up
private WritableMap getDownloadData(final UploadTask.TaskSnapshot taskSnapshot) {
  Uri downloadUrl = taskSnapshot.getDownloadUrl();
  StorageMetadata d = taskSnapshot.getMetadata();

  WritableMap resp = Arguments.createMap();
  resp.putString("downloadUrl", downloadUrl.toString());
  resp.putString("fullPath", d.getPath());
  resp.putString("bucket", d.getBucket());
  resp.putString("name", d.getName());

  WritableMap metadataObj = Arguments.createMap();
  metadataObj.putString("cacheControl", d.getCacheControl());
  metadataObj.putString("contentDisposition", d.getContentDisposition());
  metadataObj.putString("contentType", d.getContentType());
  resp.putMap("metadata", metadataObj);

  return resp;
}
 
Example #22
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutBytes() {
  mockSuccessfulResultForTask(mockUploadTask, mockUploadTaskSnapshot);
  when(mockStorageReference.putBytes(new byte[] { 1, 2, 3 })).thenReturn(mockUploadTask);
  when(mockUploadTaskSnapshot.getBytesTransferred()).thenReturn(1000L);
  when(mockUploadTaskSnapshot.getTotalByteCount()).thenReturn(1000L);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putBytes(mockStorageReference, new byte[] { 1, 2, 3 }).subscribe(obs);

  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertNoErrors();
  obs.assertComplete();
  obs.assertValue(new Predicate<UploadTask.TaskSnapshot>() {
    @Override public boolean test(UploadTask.TaskSnapshot taskSnapshot) throws Exception {
      return taskSnapshot.getBytesTransferred() == taskSnapshot.getTotalByteCount()
          && taskSnapshot.getTotalByteCount() == 1000L;
    }
  });
}
 
Example #23
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 6 votes vote down vote up
@Test public void testPutFile() {
  mockSuccessfulResultForTask(mockUploadTask, mockUploadTaskSnapshot);
  when(mockStorageReference.putFile(mockUri)).thenReturn(mockUploadTask);
  when(mockUploadTaskSnapshot.getBytesTransferred()).thenReturn(1000L);
  when(mockUploadTaskSnapshot.getTotalByteCount()).thenReturn(1000L);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putFile(mockStorageReference, mockUri).subscribe(obs);

  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertNoErrors();
  obs.assertComplete();
  obs.assertValue(new Predicate<UploadTask.TaskSnapshot>() {
    @Override public boolean test(UploadTask.TaskSnapshot taskSnapshot) throws Exception {
      return taskSnapshot.getBytesTransferred() == taskSnapshot.getTotalByteCount()
          && taskSnapshot.getTotalByteCount() == 1000L;
    }
  });
}
 
Example #24
Source File: MainActivity.java    From stockita-point-of-sale with MIT License 5 votes vote down vote up
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);


    /**
     * The code below is related to uploading image to the server storage
     */


    // If there was an upload in progress, get its reference and create a new StorageReference
    final String stringRef = savedInstanceState.getString(KEY_FOUR);
    if (stringRef == null) {
        return;
    }
    mImageStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl(stringRef);

    // Find all UploadTasks under this StorageReference (in this example, there should be one)
    List tasks = mImageStorageRef.getActiveUploadTasks();
    if (tasks.size() > 0) {

        // Get the task monitoring the upload
        UploadTask task = (UploadTask) tasks.get(0);

        // Add new listeners to the task using an Activity scope
        task.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot state) {
                // TODO: handleSuccess(state); //call a user defined function to handle the event.
            }
        });
    }

}
 
Example #25
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
@Test public void testPutBytesWithMetadata() {
  mockSuccessfulResultForTask(mockUploadTask, mockUploadTaskSnapshot);
  when(mockStorageReference.putBytes(new byte[] { 1, 2, 3 }, mockStorageMetadata)).thenReturn(
      mockUploadTask);
  when(mockUploadTaskSnapshot.getBytesTransferred()).thenReturn(1000L);
  when(mockUploadTaskSnapshot.getTotalByteCount()).thenReturn(1000L);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putBytes(mockStorageReference, new byte[] { 1, 2, 3 }, mockStorageMetadata)
      .subscribe(obs);

  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertNoErrors();
  obs.assertComplete();
  obs.assertValue(new Predicate<UploadTask.TaskSnapshot>() {
    @Override public boolean test(UploadTask.TaskSnapshot taskSnapshot) throws Exception {
      return taskSnapshot.getBytesTransferred() == taskSnapshot.getTotalByteCount()
          && taskSnapshot.getTotalByteCount() == 1000L;
    }
  });
}
 
Example #26
Source File: RxFirebaseStorageTest.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
@Test public void testPutStreamWithMetadata() {
  mockSuccessfulResultForTask(mockUploadTask, mockUploadTaskSnapshot);
  when(mockStorageReference.putStream(mockInputStream, mockStorageMetadata)).thenReturn(
      mockUploadTask);
  when(mockUploadTaskSnapshot.getBytesTransferred()).thenReturn(1000L);
  when(mockUploadTaskSnapshot.getTotalByteCount()).thenReturn(1000L);

  TestObserver<UploadTask.TaskSnapshot> obs = TestObserver.create();

  RxFirebaseStorage.putStream(mockStorageReference, mockInputStream, mockStorageMetadata)
      .subscribe(obs);

  verifyAddOnCompleteListenerForTask(mockUploadTask);

  callOnComplete(mockUploadTask);
  obs.dispose();

  callOnComplete(mockUploadTask);

  obs.assertNoErrors();
  obs.assertComplete();
  obs.assertValue(new Predicate<UploadTask.TaskSnapshot>() {
    @Override public boolean test(UploadTask.TaskSnapshot taskSnapshot) throws Exception {
      return taskSnapshot.getBytesTransferred() == taskSnapshot.getTotalByteCount()
          && taskSnapshot.getTotalByteCount() == 1000L;
    }
  });
}
 
Example #27
Source File: ImageActivity.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
private void uploadPhoto(Uri uri) {
    // Reset UI
    hideDownloadUI();
    Toast.makeText(this, "Uploading...", Toast.LENGTH_SHORT).show();

    // Upload to Cloud Storage
    String uuid = UUID.randomUUID().toString();
    mImageRef = FirebaseStorage.getInstance().getReference(uuid);
    mImageRef.putFile(uri)
            .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    if (BuildConfig.DEBUG) {
                        Log.d(TAG, "uploadPhoto:onSuccess:" +
                                taskSnapshot.getMetadata().getReference().getPath());
                    }
                    Toast.makeText(ImageActivity.this, "Image uploaded",
                                   Toast.LENGTH_SHORT).show();

                    showDownloadUI();
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "uploadPhoto:onError", e);
                    Toast.makeText(ImageActivity.this, "Upload failed",
                                   Toast.LENGTH_SHORT).show();
                }
            });
}
 
Example #28
Source File: RxFirebaseStorage.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
/**
 * @see StorageReference#putStream(InputStream, StorageMetadata)
 */
@CheckReturnValue
@NonNull
public static Single<UploadTask.TaskSnapshot> putStream(
        @NonNull final StorageReference ref,
        @NonNull final InputStream inputStream,
        @NonNull final StorageMetadata storageMetadata) {
    return RxTask.single(new Callable<Task<UploadTask.TaskSnapshot>>() {
        @Override
        public Task<UploadTask.TaskSnapshot> call() throws Exception {
            return ref.putStream(inputStream, storageMetadata);
        }
    });
}
 
Example #29
Source File: RxFirebaseStorage.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
/**
 * @see StorageReference#putFile(Uri, StorageMetadata)
 */
@CheckReturnValue
@NonNull
public static Single<UploadTask.TaskSnapshot> putFile(
        @NonNull final StorageReference ref,
        @NonNull final Uri uri,
        @NonNull final StorageMetadata storageMetadata) {
    return RxTask.single(new Callable<Task<UploadTask.TaskSnapshot>>() {
        @Override
        public Task<UploadTask.TaskSnapshot> call() throws Exception {
            return ref.putFile(uri, storageMetadata);
        }
    });
}
 
Example #30
Source File: RxFirebaseStorage.java    From rxfirebase with Apache License 2.0 5 votes vote down vote up
/**
 * @see StorageReference#putFile(Uri)
 */
@CheckReturnValue
@NonNull
public static Single<UploadTask.TaskSnapshot> putFile(@NonNull final StorageReference ref,
                                                      @NonNull final Uri uri) {
    return RxTask.single(new Callable<Task<UploadTask.TaskSnapshot>>() {
        @Override
        public Task<UploadTask.TaskSnapshot> call() throws Exception {
            return ref.putFile(uri);
        }
    });
}