无法在android 10上创建文件名已删除的文件

蜂胶

我想在下载文件夹中保留每天的日志文本文件。我想将基于日期的文本文件存储在下载文件夹中我自己的日志文件夹(MyApp 日志文件夹)中。

当我删除 My App Log 文件夹时,我无法在同一个位置用相同的名称创建这个文件夹。同样,当我创建的文本文件被删除时,我无法创建具有相同文本文件名的文件。resolver.insert(downloadUri, contentValues);总是返回空值。

即使在查询是否存在属于该路径的文件时得到空结果,我也无法创建相同的文件。

我创建文件的函数:

public static void createFile(){
    
        String contentType = "text/log";
        Date cDate = new Date(System.currentTimeMillis());
        String today = new SimpleDateFormat("yyyy_MM_dd").format(cDate);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
       
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, today + ".txt");//2021_10_13.txt
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, contentType);//text/log
        contentValues.put(MediaStore.MediaColumns.DATE_ADDED, seconds);//System.currentTimeMillis
        contentValues.put(MediaStore.MediaColumns.DATE_MODIFIED, seconds);//System.currentTimeMillis
        contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + File.separator + "MyApp Log");//Download/MyApp Log
    
        ContentResolver resolver = getContext().getContentResolver();
    
        outputUri = resolver.insert(getDownloadUri(), contentValues);
    
        if (outputUri == null)
            throw new IOException("Failed to create new MediaStore record.");
    
        try (final OutputStream stream = resolver.openOutputStream(outputUri)) {
            if (stream == null)
                return;

        } finally {
            ContentValues updateValues = new ContentValues();
            updateValues.put(MediaStore.MediaColumns.IS_PENDING, 0);
            resolver.update(outputUri, updateValues, null, null);
        }
    }

    public static @NonNull
        Uri getDownloadUri() {
            if (Build.VERSION.SDK_INT < 29) {
                return getLegacyUri(Environment.DIRECTORY_DOWNLOADS);
            } else {
                return MediaStore.Downloads.EXTERNAL_CONTENT_URI;
            }
        }

我正在查询文件是否存在的函数:

    public static Uri getExternalContentUriFromFile(Uri externalUri, String filePath) {
        if (externalUri == null)
            return null;

        try (Cursor cursor = getContentResolver().query(externalUri, new String[]{MediaStore.MediaColumns._ID},
                MediaStore.MediaColumns.DATA + "=? ", new String[]{filePath}, null)) {

            if (cursor != null && cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
                return Uri.withAppendedPath(externalUri, "" + id);
            }
            return null;
        }
    }
蜂胶

我自己找到了一个解决方案:

        int fileNo = 0;
        Uri uri = saveToUri(fileName, contentType, seconds, relativePath);
        if (uri == null) {
            while (fileNo < 4 && uri == null) {
                fileNo++;
                fileName = AttachmentUtil.removeExtensionForName(fileName) + "(" + fileNo + ")" + extension;
                uri = saveToUri(fileName, contentType, seconds, storageName + File.separator + myDirName, storageUri);
            }
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

git pull和重置因错误“无法创建文件<文件名>(文件存在)”而停止

Bash脚本无法使用数组项作为文件名创建文件

当文件名中存在unicode时,无法在python中创建文件

git checkout-<文件名>无法恢复Windows 10上本地删除的符号链接

无法在Android外部存储上创建文件

Android:无法在外部存储设备上创建文件

在Linux上无法删除文件名中的逗号

Android Studio无法创建文件

fopen()无法在Linux上创建文件

java.io.FileNotFoundException:由于文件名中的某些错误而无法创建文件,仅在某些设备(例如:android 4.1.2)中遇到此错误

在已安装文件上执行mdb-export时,“无法分配文件名”

无法创建常规文件“文件名”:文件存在

无法在Android上的外部存储上创建文件夹

Android:从网址创建正确的文件名

无法删除带有“?”的文件 在文件名中

无法删除文件名中包含特殊字符的文件

无法在Windows 10中删除路径长且奇数的文件名

无法删除带有无效字符的文件名 Windows 10

无法创建文件夹“调试” Android Studio

无法在 Android 应用程序中创建文件

无法在Android /数据中创建文件夹

无法在Android内部存储中创建文件夹

无法使用FileOutputStream类在ANDROID中创建文件

无法在Android 7.0中创建文件夹

Win 10 + Git - 无法创建文件 - 权限被拒绝

fopen无法创建文件

Python创建目录错误该文件已存在时无法创建文件

无法在 Amazon ElasticBeanstalk 上的 Python 中创建文件

无法在Windows机器的ubuntu bash shell上创建文件