-1
我嘗試在資產文件夾中讀取pdf文件存儲。我看到這個解決方案: Read a pdf file from assets folderAndroid從資產文件夾中讀取pdf文件
但像評論說這不工作了,該文件無法找到,是否有另一種解決方案直接讀取PDF文件,沒有複製PDF在外部存儲? 我不希望我的PdfRenderer最小API使用的是17
我嘗試在資產文件夾中讀取pdf文件存儲。我看到這個解決方案: Read a pdf file from assets folderAndroid從資產文件夾中讀取pdf文件
但像評論說這不工作了,該文件無法找到,是否有另一種解決方案直接讀取PDF文件,沒有複製PDF在外部存儲? 我不希望我的PdfRenderer最小API使用的是17
或許這可以成爲有用的人,所以我張貼我的答案:
private void openPdf(String pdfName){
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), pdfName);
try
{
in = assetManager.open(pdfName);
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/"+pdfName),
"application/pdf");
startActivity(intent);
}
PDF文件是資產的文件夾內的商店