0

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.

  • There is no code that saves anything. The only thing i see is that you call .insert() which will return an uri. But you are not using the uri nor did you tell if null is returned by .insert or that you got a valid uri. Please tell the value of `uri.toString()`. – blackapps May 08 '21 at 09:46
  • @blackapps getting sqlitedatabse error on reolver.insert() line only, and when logging uri.tostring() on next line getting this error, java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference. – user4193822 May 08 '21 at 10:22
  • So .insert() returns null instead of a valid uri. Please comment. Also comment on the fact that there is no code that tries to save anything. Also tell if you use an Android 10 or Android 11 device. – blackapps May 08 '21 at 10:25
  • Yes, and also giving the sqlite database error, tested in android 10 device. For saving - PdfDocument pdfDocument = new PdfDocument(new PdfWriter(uri.getPath())); – user4193822 May 08 '21 at 10:37
  • Well if you dont get a valid uri then it makes no sense to create a PdfWriter. Check uri for null before use i would say. And your problem has nothing to do with saving a pdf file but only with obtaining an uri from the media store. – blackapps May 08 '21 at 10:39
  • yes, the issue is in resolver.insert() line only, and question is how to save pdf file in android 10 and above devices. My pdf file save code for android devices above kitkat version and below android Q version is working. – user4193822 May 08 '21 at 10:44
  • `and question is how to save pdf file in android 10 and above` No. The question is `how to obtain a media store uri?` If you keep talking about saving a pdf file... sigh... concentrate on your real problem instead. – blackapps May 08 '21 at 10:45
  • `Below is my code for saving the pdf file in Downloads directory:-` Can you change that text? You are trying to obtain an uri only. – blackapps May 08 '21 at 10:49
  • Did you find a solution to this? I am having the exact same problem on 10, but works fine on 11. – Sam L Oct 11 '21 at 00:36

0 Answers0