0

I am trying to insert XSPF playlist from external storage to Android Q Media Provider database playlist table. But getting Exception.

public static void insertXSPFPlayListTable(Context context, MediaFile file) {
            //insert audio_playlists table
            Uri uri = MediaStore.Files.getContentUri("external");
            context.getContentResolver().delete(uri, "_data = ?",
                    new String[]{file.getFilePath()});
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.Audio.Playlists.DATA, file.getFilePath());
            contentValues.put(MediaStore.Audio.Playlists.DATE_MODIFIED, file.getLastModified());
            contentValues.put(MediaStore.Audio.Playlists.NAME, file.getFileName());
            Uri insertUri = context.getContentResolver().insert(
                    MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, contentValues);

        if (insertUri != null) {
            int mPlaylistId = -1;
            Cursor c = context.getContentResolver().query(insertUri, PROJECTION_PLAYLIST, null, null, null);
            if (c != null) {
                mPlaylistId = c.getInt(c.getColumnIndex(MediaStore.Audio.Playlists._ID));
                c.close();
            }
        }

        int newId = Integer.parseInt(insertUri.getLastPathSegment());
        file.setId(newId);
    }

Exception:

DatabaseUtils: java.lang.IllegalArgumentException: Requested path /storage/24E7-9DBA/testdata/PlayList/XSPF1/Test.xspf doesn't appear under [/storage/emulated/10]
05-12 13:14:36.627  3678  3697 E DatabaseUtils:     at com.android.providers.media.MediaProvider.insertFile(MediaProvider.java:2498)
05-12 13:14:36.627  3678  3697 E DatabaseUtils:     at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:3158)
05-12 13:14:36.627  3678  3697 E DatabaseUtils:     at com.android.providers.media.MediaProvider.insert(MediaProvider.java:2903)
05-12 13:14:36.627  3678  3697 E DatabaseUtils:     at android.content.ContentProvider$Transport.insert(ContentProvider.java:309)
05-12 13:14:36.627  3678  3697 E DatabaseUtils:     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:154)
05-12 13:14:36.627  3678  3697 E DatabaseUtils:     at android.os.Binder.execTransactInternal(Binder.java:1021)
05-12 13:14:36.627  3678  3697 E DatabaseUtils:     at android.os.Binder.execTransact(Binder.java:994)
user2918967
  • 1
  • 1
  • 4
  • Remove `contentValues.put(MediaStore.Audio.Playlists.DATA, file.getFilePath());`. Use the `Uri` returned by `insert()` to open an `OutputStream` and write the content into that stream. – CommonsWare Jul 06 '21 at 10:30
  • Hi @CommonsWare: Removing contentValues.put(MediaStore.Audio.Playlists.DATA, file.getFilePath()); resolve exception in insertion. But I am unable to fetch insertUri.getLastPathSegment() as path is removed from ContentValues. As per your suggestion if I open OutputStream like below: OutputStream stream = null; try { stream = context.getContentResolver().openOutputStream(insertUri); } catch (FileNotFoundException e) { e.printStackTrace(); } After that how to write playlist content on the stream? Will that help fetching getLastPathSegment() ? – user2918967 Jul 08 '21 at 04:47
  • "After that how to write playlist content on the stream?" -- um, use Java I/O. You have an `OutputStream`. So, write data to it, then close the stream when you are done. – CommonsWare Jul 08 '21 at 11:11

0 Answers0