Sni*_*per 6 android mediastore android-11
到目前为止,我检查文件是否存在,如果不存在,则将其保存到 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) 中的设备,但 Android 10 不支持它。
所以我尝试使用 mediastore API 来保存图像。
READ_STORAGE_PERMISSION -> 不允许
首先,我使用以下代码查询 contentresolver 以检查文件是否存在:
Uri collection = null;
collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
String[] PROJECTION = new String[]{MediaStore.Images.Media.DISPLAY_NAME,
        MediaStore.MediaColumns.RELATIVE_PATH};
String QUERY = MediaStore.Files.FileColumns.RELATIVE_PATH + " like ? and " + 
MediaStore.Files.FileColumns.DISPLAY_NAME + " like ?";
ContentResolver mContentResolver = this.getContentResolver();
Cursor cursor = mContentResolver.query(collection, PROJECTION, QUERY , new String[]{"%" + dirName + "%", "%" + fname + "%"}, null);
if (cursor != null) {
    Log.d("upisdk", "cursor != null");
    if (cursor.getCount() > 0) {
    } else {
   
    }
}
如果光标为空,则意味着该文件不存在,我将使用代码保存该文件
ContentResolver contentResolver = getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fname);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, dirName);
Uri imageUri = 
contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = contentResolver.openOutputStream(Objects.requireNonNull(imageUri));
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Objects.requireNonNull(fos).close();
现在,如果一切正常,直到我删除应用程序数据。
删除数据后,该文件夹仍然存在,之前保存的文件也仍然存在。现在,如果查询该文件的内容解析器,则光标为空。如果不允许 READ_STORAGE_PERMISSION,那么就会发生这种情况。如果我允许 READ_STORAGE_PERMISSION 那么它会给我非空的光标。另一方面,如果我尝试保存新的不同图像并查询解析器,那么即使没有 READ_STORAGE_PERMISSION,我也会得到非空光标。
请告诉我我做错了什么。
private fun getUriFromPath(displayName: String): Uri? {
        val photoId: Long
        val photoUri = MediaStore.Images.Media.getContentUri("external")
        val projection = arrayOf(MediaStore.Images.ImageColumns._ID)
        val cursor = contentResolver.query(
            photoUri,
            projection,
            MediaStore.Images.ImageColumns.DISPLAY_NAME + " LIKE ?",
            arrayOf(displayName),
            null
        )!!
        cursor.moveToFirst()
        val columnIndex = cursor.getColumnIndex(projection[0])
        photoId = cursor.getLong(columnIndex)
        cursor.close()
        return Uri.parse("$photoUri/$photoId")
    }
如果您愿意,您也可以添加相对路径以在特定位置查找
使用文件名检查
private fun isExist(){
        val pathOfImage=getUriFromPath("myimage.png")
        val isExist = try {
            val i= pathOfImage?.let { contentResolver.openInputStream(it) }
            i != null
        }
        catch (e: IOException) {
            e.printStackTrace()
            false
        }
        if (!isExist) {
            // do whatever u want
        }
    }
当找不到文件并尝试打开流时,此解决方案会给出异常。
在此解决方案中,我确实将文件保存到
Android\media\com.yourpackge\目录中
private fun isExist(){
        val path = this.externalMediaDirs.first()
        val folder = File(path, getString(R.string.app_name))
        if (!folder.exists()) folder.mkdirs()
        val  file = File(folder, "myimage.png")
        if (!file.exists()) {
            // save your files or do whatever you want
            // use this for show files into gallery
            MediaScannerConnection.scanFile(this, arrayOf(file.absolutePath), null,
                object : MediaScannerConnection.OnScanCompletedListener {
                    override fun onScanCompleted(path: String, uri: Uri?) {
                        Log.e("scann--","Finished scanning $path")
                    }
                })
        }
    }
现在我想将我的文件从旧getExternalStorageDirectoryAndroid\media\com.yourpackge\android 11 中的目录,就像 Whatsapp 所做的那样,无需请求任何许可。
我可以使用 kotlinUtils类将我的文件夹移动到新文件夹
sourcefolder.copyRecursively(newfolder,true)
private fun copyFiles(){
        val path = this.externalMediaDirs.first()
        val folder = File(path, getString(R.string.app_name))
        if (!folder.exists()) folder.mkdirs()
        val sourcefolder = File(Environment.getExternalStorageDirectory().toString() + "/"
                +"Supreme")
        sourcefolder.copyRecursively(folder,true)
    }
| 归档时间: | 
 | 
| 查看次数: | 4063 次 | 
| 最近记录: |