gpt4 book ai didi

android - 如何读取 Assets 文件夹中的 .pdf 文件

转载 作者:太空狗 更新时间:2023-10-29 16:22:51 25 4
gpt4 key购买 nike

File file = new File("android.resource://com.baltech.PdfReader/assets/raw/"+filename);

if (file.exists()) {
Uri targetUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(PdfReaderActivity.this, "No Application Available to View PDF", Toast.LENGTH_SHORT).show();
}

我想阅读 Assets 文件夹中的 .pdf 文件。我必须在文件名中提供什么路径。请帮助。谢谢

最佳答案

我不确定您是否已经得到答案,看起来很老,但这对我有用。

//you need to copy the input stream to a new file, so store it elsewhere
//this stores it to the sdcard in a new folder "MyApp"
String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/solicitation_form.pdf";

AssetManager assetManager = getAssets();

try {
InputStream pdfFileStream = assetManager.open("solicitation_form.pdf");
CreateFileFromInputStream(pdfFileStream, filename);

} catch (IOException e1) {
e1.printStackTrace();
}

File pdfFile = new File(filename);

CreateFileFromInputStream函数如下

public void CreateFileFromInputStream(InputStream inStream, String path) throws IOException {
// write the inputStream to a FileOutputStream
OutputStream out = new FileOutputStream(new File(path));

int read = 0;
byte[] bytes = new byte[1024];

while ((read = inStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}

inStream.close();
out.flush();
out.close();

}

真心希望这对阅读本文的其他人有所帮助。

关于android - 如何读取 Assets 文件夹中的 .pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10137131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com