Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when executing ContentResolver insert

Tags:

android

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.

like image 494
Leon Avatar asked Jun 28 '26 05:06

Leon


1 Answers

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.

like image 63
Andy Cass Avatar answered Jun 29 '26 20:06

Andy Cass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!