How can I make RELATIVE_PATH that added in android Q backward compatible?
Asked Answered
L

1

7

According to the docs the RELATIVE_PATH Added in API level 29 public static final String RELATIVE_PATH Relative path of this media item within the storage device where it is persisted. For example, an item stored at /storage/0000-0000/DCIM/Vacation/IMG1024.JPG would have a path of DCIM/Vacation/.

How can I organize my media files that being inserted to MediaStore on Android prior to Android Q Afaik: PRIMARY_DIRECTORY doesn't work put(MediaStore.MediaColumns.PRIMARY_DIRECTORY, relativePath) according to the docs it has been removed

/**
 * The primary directory name this media exists under. The value may be
 * {@code NULL} if the media doesn't have a primary directory name.
 *
 * @removed
 * @deprecated Replaced by {@link #RELATIVE_PATH}.
 */
@Column(Cursor.FIELD_TYPE_STRING)
@Deprecated
public static final String PRIMARY_DIRECTORY = "primary_directory";
Lymphadenitis answered 10/5, 2020 at 23:13 Comment(4)
"How can I organize my media files that being inserted to MediaStore on Android prior to Android Q" -- use Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), create a directory under there, and save your images using ordinary file I/O.Delegacy
I just wanted to use MediaStore API using the insert method to avoid maintaining two different implementations, is there any way to do that?Lymphadenitis
No, sorry, there is not.Delegacy
have you found any solution to this?Overtop
G
0

Basically you just need to use Media.DATA below Android Q and use Media.RELATIVE_PATH otherwise.

FYI, I wrote a function to achieve this.

Grayling answered 27/6, 2023 at 8:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.