在 Android 中读取 asset 或 raw 文件夹中的文件

2023-12-13

我必须随我的申请一起发送 pdf 文件,并将它们显示为列表,单击时每个文件都应在 Adob​​e reader 中打开。我尝试将 pdf 文件放在 /assets 文件夹中,也放在 /res/raw 文件夹中。但无论哪种情况,我都会遇到 ActivityNotFound 异常。代码和异常消息如下。

将文件放在 /assets/pdf 文件夹中

try {
            files = amanager.list("pdf");
            //AssetFileDescriptor fd = amanager.openFd(files[0]);
            Uri path = Uri.parse("android.resource://com.csg.android.myproject/assets/pdf/csgsample")  ;
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setPackage("com.adobe.reader");
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                startActivity(intent);
            }
            catch (ActivityNotFoundException e){
                Toast.makeText(FirstTab.this, "NO Viewer", Toast.LENGTH_SHORT).show();
            }


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

将文件放置在 /res/raw 文件夹中

Uri.parse("android.resource://com.csg.android.myproject/raw/csgsample")  ;

例外:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.csg.android.myproject/assets/pdf/csgsample typ=application/pdf flg=0x4000000 pkg=com.adobe.reader }

任何帮助将不胜感激。

提前致谢, 纳文


如果你得到一个ActivityNotFoundException是因为没有应用程序可以处理此类内容。因此,在这种情况下,我确信 Adob​​e Reader 已安装。

另外,我认为您必须将文件复制到 SDCard 以便其他应用程序访问它。出于安全原因,他们无法直接访问您的私人数据。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Android 中读取 asset 或 raw 文件夹中的文件 的相关文章

随机推荐