[英]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_VIEW
和FileProvider.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_DOCUMENT
和takePersistableUriPermission()
代替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.