如何从Google Drive中删除LALAOD文件并在Android中显示在图像中

发布于 2025-01-31 19:19:07 字数 1082 浏览 1 评论 0原文

这是我的以下代码。

我从Google Drive有一个问题显示图像。

源代码来自 https> https:/// www.section.io/endineering-education/backup-services-with-google-drive-api-in-android/

我也曾使用此图像url url https://drive.google.com/uc?id=file_id ,但仅在具有链接访问的任何人中工作,而不是限制图像。

fun downloadFileFromGDrive(id: String) {
    getDriveService()?.let { googleDriveService ->
        CoroutineScope(Dispatchers.IO).launch {

            val gDriveFile = googleDriveService.Files().get(id).execute()
            Log.e("gDriveFile", gDriveFile.toString())

           val outputStream: OutputStream = ByteArrayOutputStream()
            googleDriveService.files()[id].executeMediaAndDownloadTo(outputStream)

        }
    } ?: Toast.makeText(context, "Please Log In first!", LENGTH_SHORT).show()
}

This is my following code.

I'm having a issue display image from Google Drive.

Source code from https://www.section.io/engineering-education/backup-services-with-google-drive-api-in-android/

I have also worked with this image url https://drive.google.com/uc?id=FILE_ID but only worked in anyone with the link access not restricted images.

fun downloadFileFromGDrive(id: String) {
    getDriveService()?.let { googleDriveService ->
        CoroutineScope(Dispatchers.IO).launch {

            val gDriveFile = googleDriveService.Files().get(id).execute()
            Log.e("gDriveFile", gDriveFile.toString())

           val outputStream: OutputStream = ByteArrayOutputStream()
            googleDriveService.files()[id].executeMediaAndDownloadTo(outputStream)

        }
    } ?: Toast.makeText(context, "Please Log In first!", LENGTH_SHORT).show()
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ可否回来 2025-02-07 19:19:07

使用以下功能,它将下载Google Drive映像,并将其保存到App Private Files文件夹中。

 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()
                        saveImageInFilesDir(gDriveFile.id)
                    } catch (e: Exception) {
                        println("!!! Handle Exception $e")
                    }
                }
            }
        } ?: ""
    }


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

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

}

Use the following function It will download Google drive Image and save it to the app private files folder.

 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()
                        saveImageInFilesDir(gDriveFile.id)
                    } catch (e: Exception) {
                        println("!!! Handle Exception $e")
                    }
                }
            }
        } ?: ""
    }


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

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文