3

Got errors when executing insert new data into content provider,see below code, PS.I just put below codes directly into onCreate() of Activity,then execute.

ContentValues values=new ContentValues();
values.put(MediaStore.Audio.Media.TITLE,"Example Song");
values.put(MediaStore.Audio.Media.ARTIST,"Unknown");

getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,values);

just simply insert new song data, but got below error occurs.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.lastIndexOf(int)' on a null object reference
at android.os.Parcel.readException(Parcel.java:1605)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
at android.content.ContentResolver.insert(ContentResolver.java:1254)
at com.example.flover.contentproviderdemo.MainActivity.onCreate(MainActivity.java:48)

Could anyone tell me why? Thanks in advance.

Leon
  • 307
  • 3
  • 16
  • Please specify the line number 48 from your Main Activity. The error is in that line, as mentioned in your stack trace. – Varun Kumar Nov 08 '16 at 06:24
  • Line 48 is getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,values), thanks for replying – Leon Nov 08 '16 at 06:38
  • i am -almost- sure that `lastIndexOf()` is called on the `ContentProvider` class and it uses the `URI` parameter to find out which data/id/... is related to the request, but what i can't explain, why it would be null? i mean the `URI` is null? the passed value is `MediaStore.Audio.Media.EXTERNAL_CONTENT_URI` this is API constant?! how it could be null? – Yazan Nov 08 '16 at 08:57
  • Firstly thanks for ur reply, I guess the problem would be the uri which is MediaStore. Audio. Media. EXTERNAL_CONTENT_URI, but that uri is only one I found to insert. So if this way is troubled, how to insert music info such as title, artist into content provider? – Leon Nov 08 '16 at 09:05
  • Did you find a solution for this issue? – Blo Jun 26 '17 at 20:34
  • @Fllo here is solution for my question,[https://stackoverflow.com/questions/36588495/manual-add-song-to-mediastore-as-a-music-track](https://stackoverflow.com/questions/36588495/manual-add-song-to-mediastore-as-a-music-track), adding new song to android provider, you have to retrieve all the metadata and add needed info to provider database, otherwise song will be not showed. – Leon Jun 27 '17 at 04:37

1 Answers1

6

I was getting the same error pre android Q (API 29)

If you are downloading a file to the media store PRE Android Q (Api 29) include in your content values...

    val directory = mContext.getExternalFilesDir(Environment.DIRECTORY_MUSIC)

    contentValues.put(MediaStore.Audio.AudioColumns.DATA, "${directory}${song?.title}")

Api 29 and onwards omit the DATA value.

Andy Cass
  • 398
  • 6
  • 16
  • This worked for me, thank you very much. For my own understanding, is this documented somewhere? – bogdan Dec 27 '20 at 22:17
  • its working but, this way is no where mention in Add an item __https://developer.android.com/training/data-storage/shared/media#add-item__ – Mohd Qasim Dec 30 '21 at 10:57