【问题标题】:Can't create file with deleted file name at android 10无法在 android 10 上创建具有已删除文件名的文件
【发布时间】:2021-11-08 15:41:55
【问题描述】:

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

当我删除“我的应用程序日志”文件夹时,我无法在同一位置创建具有相同名称的该文件夹。同样,当我创建的文本文件被删除时,我无法创建具有相同文本文件名的文件。 resolver.insert(downloadUri, contentValues); 总是返回 null。

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

我创建文件的函数:

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;
        }
    }

【问题讨论】:

  • I cannot create a file with the same text file name. 你想在哪里这样做?每次调用 createFile() 时,毫秒都是不同的。
  • When I delete the My App Log folder 你是怎么做到的?
  • I can't create this folder in the same location with the same name. 你究竟想如何创建一个文件夹?
  • 不要在这个帖子的不同帖子上给我打电话。如果您在这里需要我,请在帖子本身给我打电话。
  • 我在下载文件夹中创建了自己的日志文件夹。 (文件夹名称:MyApp Log)。当我想从设备中手动删除此文件夹并重新创建它时,我收到上面提到的 null uri 错误。有时我会在几秒钟后尝试,有时我会在一天后尝试。

标签: android file android-10.0 android-contentresolver


【解决方案1】:

我自己找到了解决方案:

        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);
            }
        }

@Will V:

 public static String removeExtensionForName(String fileName) {
        int i = fileName.lastIndexOf('.');
        if (i > 0) {
            return fileName.substring(0, i);
        }
        return fileName;
    }

我将上述问题中的代码放入 saveToUri 函数中。

【讨论】:

  • 朋友你好,请把函数代码“saveToUri”放上去
  • 如果可以的话,贴出“AttachmentUtil”类的代码,非常感谢
猜你喜欢
  • 2020-04-10
  • 1970-01-01
  • 2012-07-08
  • 1970-01-01
  • 2010-09-06
  • 2022-01-13
  • 2017-08-26
  • 1970-01-01
  • 2011-03-18
相关资源
最近更新 更多