[英]Upload image to a google drive using google drive api programatically in android
[英]How to upload encrypted image file on google drive from android using Google Drive API
我想將加密的圖像文件從 Android 上傳到用戶特定的谷歌驅動器,還想在加載應用程序時解密圖像。
當您顯示來自谷歌驅動器的圖像時。 您需要使用解密下載圖像。
首先從谷歌驅動器 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()
}
並將其保存到應用程序私有文件夾中。
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.
有趣的顯示圖像(){
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,
)
}
}
}
使用下面的 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
}
我已經在我的 github 配置文件上使用 AES 上傳了加密和解密圖像。 請檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.