Leon
Leon

Reputation: 307

Error when executing ContentResolver insert

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.

Upvotes: 3

Views: 3637

Answers (1)

Andy Cass
Andy Cass

Reputation: 446

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.

Upvotes: 7

Related Questions