尝试将映像上载到驱动器时,中的onSuccessListner中出现接收错误



我创建了一个函数,用于将图像上传到kotlin中的驱动器。如下所示。

DriveServiceHelper类

class DriveServiceHelper(private val mDriveService: Drive) {
private val mExecutor: Executor =
Executors.newSingleThreadExecutor()
private val TAG = "DRIVE_TAG"
fun uploadFile(
localFile: java.io.File,
mimeType: String?, folderId: String?
): Any? {
return Tasks.call(mExecutor, Callable<Any> { // Retrieve the metadata as a File object.
val root: List<String>
root = folderId?.let { listOf(it) } ?: listOf("root")
val metadata =
File()
.setParents(root)
.setMimeType(mimeType)
.setName(localFile.name)
val fileContent = FileContent(mimeType, localFile)
val fileMeta =
mDriveService.files().create(
metadata,
fileContent
).execute()
val googleDriveFileHolder = GoogleDriveFileHolder()
googleDriveFileHolder.id=(fileMeta.id)
googleDriveFileHolder.name=(fileMeta.name)
googleDriveFileHolder
})
}

在我的活动中,我这样称呼它。

var mDriveServiceHelper: DriveServiceHelper? = null
private fun driveSetUp() {
val mAccount =
GoogleSignIn.getLastSignedInAccount(this)
val credential = GoogleAccountCredential.usingOAuth2(
applicationContext, setOf(Scopes.DRIVE_FILE)
)
credential.selectedAccount = mAccount!!.account
googleDriveService = Drive.Builder(
AndroidHttp.newCompatibleTransport(),
GsonFactory(),
credential
)
.setApplicationName("GoogleDriveIntegration 3")
.build()
mDriveServiceHelper = DriveServiceHelper(googleDriveService)
}

private fun uploadImageIntoDrive() {
driveSetUp()
val TAG = "image upload"
val bitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, arrayList[0].uri)
try {
if (bitmap == null) {
Log.i(TAG, "Bitmap is null")
return
}
val file =
File(applicationContext.filesDir, "FirstFile")
val bos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos)
val bitmapdata = bos.toByteArray()
//write the bytes in file
val fos = FileOutputStream(file)
fos.write(bitmapdata)
fos.flush()
fos.close()
mDriveServiceHelper!!.uploadFile(file, "image/jpeg", null)
.addOnSuccessListener(OnSuccessListener<GoogleDriveFileHolder> { googleDriveFileHolder ->
Log.i(
TAG,
"Successfully Uploaded. File Id :" + googleDriveFileHolder.id)
})
.addOnFailureListener(OnFailureListener { e ->
Log.i(
TAG,
"Failed to Upload. File Id :" + e.message
)
})
} catch (e: Exception) {
Log.i(TAG, "Exception : " + e.message)
}
}

但问题在于uploadImageTodrive((函数addOnSuccessListener显示为红色,并显示未解决引用:addOnSuchListener

请帮助

您的助手类返回类型Any?:

fun uploadFile(
localFile: java.io.File,
mimeType: String?, folderId: String?
): Any? { ... // Look at the return type

但它实际上应该返回Task<GoogleDriveFileHolder>:

fun uploadFile(
localFile: java.io.File,
mimeType: String?, folderId: String?
): Task<GoogleDriveFileHolder> { ... // Non-optional Task<GoogleDriveFileHolder>

相关内容

  • 没有找到相关文章

最新更新