[英]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.