Writing files to publicly accessible documents folder in Android Q - Scoped Storage

后端 未结 2 1617
旧巷少年郎
旧巷少年郎 2020-12-15 01:14

Background

After migrating to Android Q I can no longer find a suitable way to gain write access to the documents folder (/storage/emulated/0/Documents

相关标签:
2条回答
  • 2020-12-15 02:09

    For Android Q only.

    String collection = "content://media/1c11-2404/file"; // One can even write to micro SD card
    String relative_path = "Documents/MyFolder";
    
    Uri collectionUri = Uri.parse(collection);
    
    ContentValues contentValues = new ContentValues();
    
    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName);
    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
    contentValues.put(MediaStore.MediaColumns.SIZE, filesize);
    contentValues.put(MediaStore.MediaColumns.DATE_MODIFIED, modified );
    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relative_path);
    contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1)
    
    Uri fileUri = context.getContentResolver().insert(collectionUri, contentValues);
    

    Having an uri one can open an output stream to write the content for the file.

    0 讨论(0)
  • 2020-12-15 02:12

    try to put this line in your manifest file for android Q: android:requestLegacyExternalStorage="true" in application tag.

    0 讨论(0)
提交回复
热议问题