I created a pdf file using itext pdf library and saving the generated pdf file in downloads directory. But it's not getting saved in devices running android api level 29 and above, error given below for the same.
E/SQLiteDatabase: Error inserting bucket_display_name=Download owner_package_name=com.xyz.maninventory parent=12 volume_name=external_primary _display_name=invoice.pdf mime_type=application/pdf _data=/storage/emulated/0/Download/invoice.pdf title=invoice group_id=1960198957 is_download=true position=0 date_added=1620469149 primary_directory=Download bucket_id=540528482 media_type=10003 relative_path=Download/
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: files._data (code 2067 SQLITE_CONSTRAINT_UNIQUE)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:879)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:88)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1625)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1494)
at com.android.providers.media.MediaProvider.insertFile(MediaProvider.java:3455)
at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:4085)
at com.android.providers.media.MediaProvider.insert(MediaProvider.java:3674)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:313)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:180)
at android.os.Binder.execTransactInternal(Binder.java:1021)
at android.os.Binder.execTransact(Binder.java:994)
Below is my code for creating new pdf file in Downloads directory:-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver=getContentResolver();
ContentValues contentValues=new ContentValues();
contentValues.put(MediaStore.Downloads.DISPLAY_NAME,"invoice.pdf");
contentValues.put(MediaStore.Downloads.MIME_TYPE,"application/pdf");
Uri uri = resolver.insert(MediaStore.Downloads.getContentUri("external"), contentValues);
}else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "invoice.pdf");
}
Thanks in advance.