Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将pdf从资产传递到SD卡_Android - Fatal编程技术网

Android 将pdf从资产传递到SD卡

Android 将pdf从资产传递到SD卡,android,Android,我正在尝试将原始文件夹中的pdf文件传递给sdcard,以便以默认意图打开它 当我打开它时,它告诉我pdf被损坏了。因为我不太了解流是如何工作的,所以我认为问题来自于这里 final String extStorageDirectory = Environment .getExternalStorageDirectory().toString(); final String festivalDirectory_pat

我正在尝试将原始文件夹中的pdf文件传递给sdcard,以便以默认意图打开它

当我打开它时,它告诉我pdf被损坏了。因为我不太了解流是如何工作的,所以我认为问题来自于这里

final String extStorageDirectory = Environment
                        .getExternalStorageDirectory().toString();
                final String festivalDirectory_path = extStorageDirectory
                        + Constants.PDF_STORAGE_PATH;
                File pdfOutputFile = new File(festivalDirectory_path, "/");
                if (pdfOutputFile.exists() == false) {
                    pdfOutputFile.mkdirs();
                }
                File pdfFile = new File(pdfOutputFile, nameOfThePdf);

                try {
                    InputStream in = getResources().openRawResource(R.raw.blablublo);

                    FileOutputStream out = new FileOutputStream(pdfFile);
                    byte[] buffer = new byte[1024];
                    int bufferLength = 0;
                    while ((bufferLength = in.read(buffer)) > 0) {
                        out.write(buffer, 0, bufferLength);
                    }
                    out.close();
                } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                }
                Uri path = Uri.fromFile(pdfFile);
//              Uri path = Uri.parse(Constants.SPLASH_URI + R.raw.cartecannotpdf);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
希望有人能帮助我

thx


雷诺

看来你的解决方案完成了一半

InputStream InputStream=assetManager.open(“cartecontpdf.pdf”)-创建一个流,可用于读取资产文件夹中PDF文件中的所有数据

File File=new文件(dir,“cartecannotpdf.pdf”)-这只是创建一个指向SD卡上文件所在位置的链接

您缺少的部分实际上是将字节从inputstream复制到outputstream。目前,SD卡上的文件要么不存在,要么只是一个空壳(没有任何数据),因此您的意图会抱怨它已损坏

这是一个为您提供完整解决方案的QA。


编辑:此外,我不确定您是否需要将文件放在资产文件夹中,或者是否可以将其保留在原始文件夹中。我一直在处理我的资产文件夹中的文件。当您调用assetManager.open(…)时,请确保您实际上获得了一个文件。

您没有复制该文件。看一看thx,我已经看了这个链接并修改了我的代码,但是我必须错过一些其他的东西。现在是什么问题?尝试隔离问题-检查复制的文件是否可以读取(例如,通过下载到您的计算机)。pdf仍然没有显示在我的目标文件夹中(我在手机中浏览)。好的,很抱歉延迟,我正试图修改我的代码,正如你们所说,它仍然不起作用。我想我现在很好地填写了SD卡文件。我看不到其他阅读pdf的方法1)尝试调用
out.flush()
before
out.close()
to
OutputStream out=新文件OutputStream(pdfFile)
First:在该方法的开头设置一个断点,并逐步遍历每一行代码。我有一种感觉,您正在遇到异常,但因为它们被记录,所以您没有注意到。将PDF文件移动到资产文件夹,而不是原始文件夹。如果您想将它保存在RAW文件夹中,您需要以其他方式访问它,请参见以下内容:好的,这确实是我加入我的资源的方式,使用getRaw,它工作了!非常感谢