【发布时间】:2018-05-05 12:49:32
【问题描述】:
我想从移动内部存储中选择 .pdf 文件,将此文件保存在 sqlite 中作为 blob 类型。当我点击“查看”按钮时,然后从数据库中检索它并在支持 pdf 的应用程序中打开它。
这里我用这个代码来挑选文件
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 100);
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();
然后将此 byteFile 作为 blob 类型保存到数据库中。现在我该如何检索和打开它。请帮帮我
【问题讨论】:
标签: java android sqlite file-handling