简体   繁体   English

如何从文档中选择pdf文件,将其存储在SQLite中,然后再次从SQLite中打开它?

[英]How to pick pdf file from document , store it in SQLite and again open it from SQLite?

I want to pick .pdf file from mobile internal storage , save this file in sqlite as blob type. 我想从移动内部存储中选择.pdf文件,将此文件保存为sqlite作为Blob类型。 When i click on 'View' button then retrieve it from database and open it in pdf supported application. 当我单击“查看”按钮,然后从数据库中检索它并在pdf支持的应用程序中将其打开。

Here i used this code for pick file 在这里我使用此代码来选择文件

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("application/pdf");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, 100);

code of onActivityResult onActivityResult的代码

                Uri fileuri = data.getData();
                Log.d(TAG, "Document File Uri = " + fileuri);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                FileInputStream fis;
                try
                {
                    fis = new FileInputStream(new File(fileuri.getPath()));
                    byte[] buf = new byte[1024];
                    int n;
                    while (-1 != (n = fis.read(buf)))
                        baos.write(buf, 0, n);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                byte[] byteFile = baos.toByteArray();

then this byteFile saved into database as a blob type. 然后将此byteFile作为Blob类型保存到数据库中。 Now how can i retrieve and open it. 现在我该如何找回并打开它。 Please help me 请帮我

code of onActivityResult onActivityResult的代码

A Uri is not a file. Uri不是文件。 Only if the scheme of the Uri is file does your code work. 仅当Uri方案为file ,您的代码才起作用。 Replace your FileInputStream with an InputStream , where you get that InputStream from getContentResolver().openInputStream(data.getData()) . InputStream替换FileInputStream ,从getContentResolver().openInputStream(data.getData())获得该InputStream

Also: 也:

  • Your app will crash a lot, as you will not have enough memory to hold the PDF 您的应用将崩溃很多,因为您没有足够的内存来容纳PDF

  • Saving large files in BLOB columns is an anti-pattern in Android, as the SQLite APIs that we use do not handle large contents very well 在Android中,将大型文件保存在BLOB列中是一种反模式,因为我们使用的SQLite API不能很好地处理大型内容

  • The user will not be happy with you, as you are doing this disk I/O on the main application thread, causing the UI of your app to freeze while that I/O is being performed 用户将对您不满意,因为您正在主应用程序线程上执行此磁盘I / O,从而导致在执行I / O时冻结应用程序的UI

Now how can i retrieve and open it 现在我该如何找回并打开它

Reverse the process: 颠倒过程:

  • Get the byte[] from the database 从数据库中获取byte[]

  • Save that byte[] to a file 将该byte[]保存到文件中

  • Use ACTION_VIEW and FileProvider.getUriForFile() to open the file in the user's chosen PDF viewer 使用ACTION_VIEWFileProvider.getUriForFile()在用户选择的PDF查看器中打开文件

Once again: 再来一次:

  • Your app will crash a lot, due to running out of memory trying to load in a large BLOB 您的应用会崩溃,原因是试图加载大型BLOB的内存不足

  • Do the I/O on a background thread, so as not to freeze the UI 在后台线程上执行I / O,以免冻结UI

  • Storing large BLOB values is an anti-pattern in Android 存储较大的BLOB值是Android中的反模式

I strongly recommend that you not store the PDF as a BLOB in a database. 我强烈建议您不要将PDF作为BLOB存储在数据库中。 Either: 或者:

  • Use ACTION_OPEN_DOCUMENT and takePersistableUriPermission() instead of ACTION_GET_CONTENT , and save the Uri in the database, or 使用ACTION_OPEN_DOCUMENTtakePersistableUriPermission()代替ACTION_GET_CONTENT ,并将Uri保存在数据库中,或者

  • Copy the PDF to a file, and store some identifier of the file in the database 将PDF复制到文件,并将该文件的某些标识符存储在数据库中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从图库中选择图像并将其存储到 SqLite 数据库中 - How to pick an image from gallery and store into SqLite database 如何从 SQlite 数据库中的可绘制对象存储 ID 并再次检索它? - How to store ID from a drawable in SQlite database and retrieve it again? Android:如何将相机和外部存储中的图像存储到SQLite中,然后又将其取回? - Android: How to store an image from camera and external storage into SQLite and also get it back again? 如何从sqlite检索列并将其存储到数组中? - How to retrieve a column from sqlite and store it into an array? 我如何从Android的sqlite数据库中存储的Blob中打开pdf? - How do i open a pdf from a blob stored in sqlite database in android? 如何将 JPEG 文件路径从内部存储存储到 SQLite 数据库以及将其存储为什么数据类型? - How to store JPEG file path from internal storage to SQLite database & what data type to store it as? 从SFTP文件夹中选择一个csv / xls文件进行一些更新并再次存储到SFTP文件夹 - Pick a csv/xls file from SFTP Folder do some update and store again to SFTP folder 我可以从数据库 sqlite 存储和检索.kml 文件吗 - Can I store and retrieve .kml file from database sqlite 如何从sqlite for Android将字符串数据存储到整数数组中 - how to store string data into integer array from sqlite for android 如何从sqlite存储和检索BLOB类型? - How do I store and retrieve a BLOB type from sqlite?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM