构建 android 应用程序时在哪里存储静态 pdf 或一般文件?

Where to store static pdf or general files while building an android app?

我有许多 pdf 文件(但我也可能将功能扩展到其他文档类型),我想在我的应用程序中显示这些文件。静态图像放在可绘制文件夹中,静态文本放在字符串文件中。但是我应该把 pdf 文件放在哪里呢?

我知道我可以将它托管在服务器上并进行一次性下载,但对于我的应用程序用例来说这是不可能的。该应用程序是为一个非常具体的用例而设计的,我绝对需要将 pdf 文件与该应用程序捆绑在一起。

在您的应用程序中创建名为 assets 的目录,并将您的 pdf 文件放在该目录中。使用 this 阅读和显示 pdf 文件。

我认为您可以将 PDF 文件(或任何其他文件类型)保存在资产文件夹中。因此,在下载 APK 表单商店时,它也会下载这些文件。唯一的问题是它会增加应用程序的大小。

检查以下答案如何使用 assetManager

从资产访问 PDF 文件

我正在回答我自己的问题,因为其他答案对我来说并不完全有效。

像其他答案一样,我从资产文件夹中读取文件并在内部存储中创建了一个文件。我使用 muPDF 与文件 URI 完美配合。

items 是文件名的 ArrayList。

try
{
    AssetManager assetManager = getAssets();
    InputStream in = assetManager.open(items.get(position));
    byte[] buffer = new byte[in.available()];
    in.read(buffer);
    File targetFile = new File(getFilesDir(), items.get(position));
    OutputStream outStream = new FileOutputStream(targetFile);
    outStream.write(buffer);
    in.close();
    outStream.flush();
    outStream.close();

    //Change below intent statements as per your code
    Intent intent = new Intent(this, DocumentActivity.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(Uri.fromFile(targetFile));
    startActivity(intent);
}
catch (Exception e)
{
    e.printStackTrace();
}