如何解决如何将 pdf 文件保存在 android api 级别 29 及以上的下载目录中
我使用 itext pdf 库创建了一个 pdf 文件,并将生成的 pdf 文件保存在下载目录中。但它没有保存在运行 android api 级别 29 及更高级别的设备中,下面给出了相同的错误。
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)
以下是我在下载目录中创建新 pdf 文件的代码:-
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");
}
提前致谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。