android复制文件显示错误



它显示07-07 16:34:48.270:W/System.err(6050):copy java.io.IOException我已经给许可了

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 

文件复制代码:

  File file = new File("/mnt/sdcard/infobooks/");
            if (file.exists() == false) {
                file.mkdirs();
            }
            InputStream myInput = this.getAssets().open("Book4.pdf");
            String outFileName = Environment.getExternalStorageDirectory()+"/infobooks/Book4.pdf";
            File file2=new File(outFileName);
            file2.createNewFile();
            FileOutputStream myOutput = new FileOutputStream(outFileName);
            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();

听上去,您的错误与写入外部存储无关,而是从资产中读取。资产目录中是否存在文件"Book4.pdf"?它有多大?你运行的是什么版本的安卓系统?曾经有一段时间,当使用压缩时,Android对资产中的文件大小有限制。在Android 2.3之前,AssetManager无法读取大型压缩文件(其中大型意味着在未压缩状态下超过1MB)。解决方法是不压缩文件(通过给它一个aapt跳过压缩的扩展名,如jpg或png),转到命令行构建,或者将文件拆分成更小的块,然后在设备上重新构建它们。

然而,如果没有完整的堆栈跟踪,我们就无法真正知道错误的原因

相关内容

最新更新