1

I want to upload a photo to Google-Photo using google-api-client. Here is my code:

gradle file:

    api 'com.google.api-client:google-api-client:1.26.0'
    api 'com.google.api-client:google-api-client-android:1.26.0'
    api 'io.grpc:grpc-okhttp:1.16.1'
    api 'com.google.photos.library:google-photos-library-client:1.0.1'
    api 'com.google.android.gms:play-services-auth:16.0.1'
    api 'com.google.android.gms:play-services-drive:16.0.0'

Initialize photo-client:

private fun initializePhotoClient(): PhotosLibraryClient? {
    val token = authRepository.getAccessToken()
    val credentials = googleAuth.getUserCredentials(token) ?: return null
    val settings = PhotosLibrarySettings.newBuilder()
            .setCredentialsProvider { credentials }.build()
    return PhotosLibraryClient.initialize(settings)
}

uploadFile:

val fileName = localFileProvider.getFileName(uri)
            val uploadRequest = UploadMediaItemRequest.newBuilder()
                    .setFileName(fileName)
                    .setDataFile(RandomAccessFile(uri, "r"))
                    .build()
            val uploadResponse = photosLibraryClient.uploadMediaItem(uploadRequest)

uploadMediaItem method always return error as below. Does anyone know how to fix this? Please give me a hint.

10-27 22:05:47.881 22671-22767/com.smilebooth.easyshare E/AndroidRuntime: FATAL EXCEPTION: android_2
    Process: com.smilebooth.easyshare, PID: 22671
    java.lang.NoSuchMethodError: No static method catching(Lcom/google/common/util/concurrent/ListenableFuture;Ljava/lang/Class;Lcom/google/common/base/Function;)Lcom/google/common/util/concurrent/ListenableFuture; in class Lcom/google/common/util/concurrent/Futures; or its super classes (declaration of 'com.google.common.util.concurrent.Futures' appears in /data/app/com.smilebooth.easyshare-_OaEo0xawrKXOX90QnGu-Q==/split_lib_dependencies_apk.apk!classes2.dex)
        at com.google.api.core.ApiFutures.catching(ApiFutures.java:77)
        at com.google.photos.library.v1.upload.PhotosLibraryUploadUnaryCallable.futureCall(PhotosLibraryUploadUnaryCallable.java:50)
        at com.google.photos.library.v1.upload.PhotosLibraryUploadUnaryCallable.futureCall(PhotosLibraryUploadUnaryCallable.java:31)
        at com.google.api.gax.rpc.UnaryCallable.futureCall(UnaryCallable.java:87)
        at com.google.api.gax.rpc.UnaryCallable.call(UnaryCallable.java:112)
        at com.google.photos.library.v1.PhotosLibraryClient.uploadMediaItem(PhotosLibraryClient.java:125)
        at com.smilebooth.data.repository.ShareDataRepository.uploadPhotoContent(ShareDataRepository.kt:201)
        at com.smilebooth.data.repository.ShareDataRepository.share(ShareDataRepository.kt:104)
        at com.smilebooth.domain.usecase.ShareUseCase$buildUseCaseObservable$1.subscribe(ShareUseCase.kt:21)
        at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
        at io.reactivex.Observable.subscribe(Observable.java:12090)
        at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
        at io.reactivex.internal.schedulers.ExecutorScheduler$ExecutorWorker$BooleanRunnable.run(ExecutorScheduler.java:260)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

UPDATED: Add the function that initializes the photosLibraryClient as @Ricardo required. The photo-client works as I can create an album with no issue

Gauthier
  • 4,798
  • 5
  • 34
  • 44
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165

2 Answers2

0

Downgrading the grpc-okhttp library made it work for me. I use this version:

implementation 'io.grpc:grpc-okhttp:1.15.1'

Something must have changed in the Future class since then and the Google Photos library didn't update to work with the changes yet.

Gauthier
  • 4,798
  • 5
  • 34
  • 44
0

Using these dependencies fixed issue for me

implementation 'io.grpc:grpc-netty:1.50.0'
implementation 'io.grpc:grpc-protobuf:1.50.0'
implementation 'io.grpc:grpc-stub:1.50.0'
implementation 'io.grpc:grpc-okhttp:1.50.0'
implementation 'io.grpc:grpc-core:1.50.0'

//Google photos API. Add if required
implementation("com.google.photos.library:google-photos-library-client:1.7.3")
Tarun Anchala
  • 2,232
  • 16
  • 15