简体   繁体   English

如何使用 Google Drive API 从 android 将加密的图像文件上传到 Google Drive

[英]How to upload encrypted image file on google drive from android using Google Drive API

I want to upload the encrypted image file from Android to a user-specific google drive, Also want to Decrypt the image while loading in-app.我想将加密的图像文件从 Android 上传到用户特定的谷歌驱动器,还想在加载应用程序时解密图像。

and when you display image from google drive.当您显示来自谷歌驱动器的图像时。 you need to downlaod the image using decryption.您需要使用解密下载图像。

First download the image from google drive function call.`首先从谷歌驱动器 function 调用下载图像。`

 fun downloadFileFromGDrive(id: String) {
    getDriveService()?.let { googleDriveService ->
        CoroutineScope(Dispatchers.IO).launch {
            Log.e("idDownload", id)
            val file = File(context.filesDir, "${id}.jpg")
            if(!file.exists()) {
                try {
                    val gDriveFile = googleDriveService.Files().get(id).execute()
                    createDirectoryAndSaveImagePackage(gDriveFile.id)
                } catch (e: Exception) {
                    println("!!! Handle Exception $e")
                }
            }
        }
    } ?: ""
    //Toast.makeText(context, "Please Log In first!", LENGTH_SHORT).show()
}

and save it to the app private files folder.并将其保存到应用程序私有文件夹中。

 fun createDirectoryAndSaveImagePackage(id: String?) {
    getDriveService()?.let { googleDriveService ->
        CoroutineScope(Dispatchers.IO).launch {
            val file = File(context.filesDir, "${id}.jpg")
            Log.e("fileEncryptedDirGD", "$file")
            try {
                val outputStream = FileOutputStream(file)
                googleDriveService.files()[id]
                    .executeMediaAndDownloadTo(outputStream)
                if (id != null) {
                    googleDriveService.readFile(id)
                }
                val decryptedDataDir = ImageCrypto().decryptFile("$file")
                Log.e("decryptedDataDir", decryptedDataDir)
                outputStream.flush()
                outputStream.close()

            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}


  and display image jetpack compose.

fun displayImage() {有趣的显示图像(){

val fileGD = File(context.filesDir, "${it1}.jpg") if (fileGD.exists()) { val decryptedData2 = ImageCrypto().decryptFile("$fileGD") val fileGD = File(context.filesDir, "${it1}.jpg") if (fileGD.exists()) { val decryptedData2 = ImageCrypto().decryptFile("$fileGD")

                                       val painterBitmap = rememberImagePainter(
                                        data = File(decryptedData2),
                                        builder = {
                                            crossfade(500)
                                        })

                                    val painterState = painterBitmap.state

                                    val painter =
                                        rememberImagePainter(
                                            data = File(decryptedData2),
                                            builder = {
                                                placeholder(R.drawable.placeholder)
                                                error(R.drawable.placeholder)
                                            })


                                    if (painterState is ImagePainter.State.Loading) {
                                        CircularProgressIndicator(
                                            modifier = Modifier.align(Alignment.Center),
                                            color = MaterialTheme.colors.secondary
                                        )

                                    } else {

                                        Image(
                                            painter = painter,
                                            contentScale = ContentScale.Inside,
                                            modifier = Modifier
                                                .width(200.dp)
                                                .height(100.dp)
                                                .padding(PADDING / 2),
                                            contentDescription = null,
                                        )
                                    }
                                   }
                                   }
  

Use the following function It will upload encrypted image file on google drive and also downloading the decryption image save it to the app private files folder.使用下面的 function 它将在谷歌驱动器上上传加密的图像文件,并下载解密图像并将其保存到应用程序的私有文件夹中。

suspend fun uploadFileToGDrive(path: String?, sufix: String?): String {
    getDriveService()?.let { googleDriveService ->
        try {
            if (path != null) {
                Log.e("pathGD", path)
            }

            googleDriveService.fetchOrCreateAppFolder(
                context.getString(R.string.application_folder),
                preferenceHelper
            )
            val encryptedData = ImageCrypto().encryptFile("$path")
            Log.e("encryptedData", encryptedData)

            val actualFile = File(encryptedData)
            if (!actualFile.exists()) error("File $actualFile not exists.")
            val gFile = com.google.api.services.drive.model.File()
            // gFile.name = actualFile.name
            val formatter = SimpleDateFormat("yyyyMMddHHmmss")
            var dateString = formatter.format(Date())

            gFile.name = dateString + sufix
            gFile.parents = listOf(preferenceHelper.getFolderId())
          
            val fileContent = FileContent("image/jpeg", actualFile)
            val create = googleDriveService.files().create(gFile, fileContent)
                .setFields("id, parents")
                .execute()
            driveFilePathId = create.id

        } catch (exception: Exception) {
            exception.printStackTrace()
        }
    } ?: ""
   Toast.makeText(context, "Please Log In first!", LENGTH_LONG).show()
    return driveFilePathId
}

and I have uploaded encryption and decryption image using AES on my github profile.我已经在我的 github 配置文件上使用 AES 上传了加密和解密图像。 please check.请检查。

https://github.com/meshramaravind/FileEncryptedAES https://github.com/meshramaravind/FileEncryptedAES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中以编程方式使用Google Drive API将图像上传到Google Drive - Upload image to a google drive using google drive api programatically in android 在 Android Studio 中使用 Google Drive API 从驱动器下载文件以上传到另一个驱动器 - Download file from drive to upload into another drive using Google Drive API in Android Studio 如何将加密文件上传到Google驱动器 - how to upload encrypted file to google drive 如何使用Google Drive Android API删除Google Drive上的文件 - How to delete a file on google drive using Google Drive Android API Google Drive Android API如何将音频文件上传到我的驱动器? 如何同步驱动器文件? - Google Drive Android API how to upload a audio file to my drive ? How to sync drive files? 使用 Google 登录从 Android 设备将文件上传到 Google Drive - Upload a file to Google Drive using Google Sign In from Android device 如何使用Drive.API从Google云端硬盘下载文件? - How to download file from Google Drive using Drive.API? 从Android上传Google云端硬盘图片 - Google Drive image upload from android 将图像从Android应用程序上传到Google驱动器 - Upload image from android application to google drive 使用Android版Google Drive API将JSON文件上传到Google Drive - Upload JSON file to Google Drive using Google Drive API for android programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM