Android Q Kotlin-API 29:检查图像是否存在

Android Q Kotlin-API 29:检查图像是否存在,android,kotlin,android-10.0,Android,Kotlin,Android 10.0,这是我用来在Android Q中保存图像的代码: private var fileName = "" private fun downloadImage(){ val folderName = "Funny" fileName = "bla_" + dlImageURL.split("/").toTypedArray().last() // find out here if this image already exists val requestOption

这是我用来在Android Q中保存图像的代码:

private var fileName = ""
private fun downloadImage(){
    val folderName = "Funny"
    fileName = "bla_" + dlImageURL.split("/").toTypedArray().last()

    // find out here if this image already exists

    val requestOptions = RequestOptions()
        .skipMemoryCache(true)
        .diskCacheStrategy(DiskCacheStrategy.NONE)


    val bitmap = Glide.with(this@DownloadImage)
        .asBitmap()
        .load(dlImageURL)
        .apply(requestOptions)
        .submit()
        .get()



    try {

        val values = ContentValues()
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
        values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000)
        values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/$folderName")
        values.put(MediaStore.Images.Media.IS_PENDING, true)
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis())
        values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
        values.put(MediaStore.Images.Media.TITLE, fileName)
        // RELATIVE_PATH and IS_PENDING are introduced in API 29.

        val uri: Uri? = this@DownloadImage.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

        if (uri != null) {
            saveImageToStream(bitmap, this@DownloadImage.contentResolver.openOutputStream(uri))
            values.put(MediaStore.Images.Media.IS_PENDING, false)
            this@DownloadImage.contentResolver.update(uri, values, null, null)
        }

    } catch (e: Exception) {

    }
}



private fun saveImageToStream(bitmap: Bitmap, outputStream: OutputStream?) {
    if (outputStream != null) {
        try {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 95, outputStream)
            outputStream.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
} 
在下载之前,我如何确定此图像是否已存在于库中?我评论了它应该在哪里


我已经查过了,但找不到如何获取该死的路径,这要容易得多我用ContentResolver.query()方法编写了示例代码。所以我尝试了Android Q。示例如下:

val projection = arrayOf(
                    MediaStore.Images.Media.DISPLAY_NAME,
                    MediaStore.MediaColumns.RELATIVE_PATH
    )

    val path = "Pictures/$folderName"
    val name = fileName

    val selection = MediaStore.Files.FileColumns.RELATIVE_PATH + " like ? and "+ MediaStore.Files.FileColumns.DISPLAY_NAME + " like ?"

    val selectionargs = arrayOf("%" + path + "%", "%" + name + "%")
    val cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionargs,  null);

    val indexDisplayName = cursor?.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)

    if(cursor!!.count > 0){
        // file is exist
    }

    // or you can see displayName 
    while (cursor!!.moveToNext()) {
        val displayName = indexDisplayName?.let { cursor.getString(it) }
    }

你对“已经存在”的定义是什么?存在于哪里?在Glide的内存缓存中?在gallery的“Funny”文件夹中,知道如何操作的人将通过代码查看图像的保存位置。我将“在gallery”添加到问题中,尝试使用所需的
相对路径查询相同的
MediaStore
Uri
,看看是否匹配。现在我尝试了你的建议(当我理解正确时):val check1=MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI.toString()+“/”+“Pictures/”+folderName+“/”+fileName if(文件(check1).exists()){在Android 10的作用域存储中,如果我清除数据并尝试查询解析器,它会告诉我该文件不存在,但该文件在文件夹中。在授予READ_storage_权限后,我会得到光标,但如果我删除它,则会得到空光标。