构建Android应用时,在哪里存储静态pdf或常规文件?

问题描述

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

我知道我可以将其托管在服务器上,并且可以一次性下载,但是对于我的应用程序而言,这是不可能的。该应用是为特定用途设计的,我绝对需要将pdf文件与该应用捆绑在一起。

解决方法

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

,

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

检查以下答案,您如何使用 assetManager

从资产访问PDF文件

https://stackoverflow.com/a/17085759/7023751

,

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

与其他答案一样,我从资产文件夹中读取了文件,并在内部存储中创建了一个文件。我使用了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();
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...