无法在外部存储设备上编写pdf

时间:2016-06-24 13:47:21

标签: java android file-io io

我无法将从url下载的pdf文件写入android存储。 我已阅读本网站上的所有相关问题,但仍然无法弄清楚我的代码是否有错误。

错误在于 pdfFile.createNewFile();在try {}块中。

private class DownloadFile extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... strings) {
            String fileUrl = strings[0];  
            String fileName = strings[1];  
            String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            File folder = new File(extStorageDirectory, "/test/");
            folder.mkdirs();

            File pdfFile = new File(folder, fileName);

            try{
                pdfFile.getParentFile().mkdirs();
                pdfFile.createNewFile();
            }catch (IOException e){
                e.printStackTrace();
            }
            FileDownloader.downloadFile(fileUrl, pdfFile);
            return null;
        }

我的Logcat

06-24 19:11:26.886 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err: java.io.IOException: open failed: ENOENT (No such file or directory)
06-24 19:11:26.886 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.io.File.createNewFile(File.java:939)
06-24 19:11:26.886 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at in.ac.iitd.bsw.iitdapp.VolleyDownload$DownloadFile.doInBackground(VolleyDownload.java:112)
06-24 19:11:26.888 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at in.ac.iitd.bsw.iitdapp.VolleyDownload$DownloadFile.doInBackground(VolleyDownload.java:96)
06-24 19:11:26.888 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
06-24 19:11:26.888 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-24 19:11:26.889 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
06-24 19:11:26.889 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
06-24 19:11:26.889 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
06-24 19:11:26.889 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.lang.Thread.run(Thread.java:818)
06-24 19:11:26.889 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
06-24 19:11:26.889 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at libcore.io.Posix.open(Native Method)
06-24 19:11:26.890 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
06-24 19:11:26.890 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.io.File.createNewFile(File.java:932)
06-24 19:11:26.890 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     ... 8 more
06-24 19:11:26.890 22920-22999/in.ac.iitd.bsw.iitdapp W/System: ClassLoader referenced unknown path: /system/framework/tcmclient.jar
06-24 19:11:27.040 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err: java.io.FileNotFoundException: /storage/emulated/0/test/maven.pdf: open failed: ENOENT (No such file or directory)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at in.ac.iitd.bsw.iitdapp.FileDownloader.downloadFile(FileDownloader.java:27)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at in.ac.iitd.bsw.iitdapp.VolleyDownload$DownloadFile.doInBackground(VolleyDownload.java:116)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at in.ac.iitd.bsw.iitdapp.VolleyDownload$DownloadFile.doInBackground(VolleyDownload.java:96)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
06-24 19:11:27.041 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
06-24 19:11:27.042 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at java.lang.Thread.run(Thread.java:818)
06-24 19:11:27.042 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
06-24 19:11:27.043 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at libcore.io.Posix.open(Native Method)
06-24 19:11:27.043 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
06-24 19:11:27.043 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
06-24 19:11:27.043 22920-22999/in.ac.iitd.bsw.iitdapp W/System.err:     ... 11 more

我在Android Manifest中正确设置了权限

2 个答案:

答案 0 :(得分:0)

我在运行Android 6.0 Marshmallow的设备上测试它,这需要额外的权限才能访问敏感数据。

信用 - @CommonsWare Android permission doesn't work even if I have declared it

此方法可用于询问用户的权限。 原始答案 - https://stackoverflow.com/a/33162451/4420961

public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }


}

答案 1 :(得分:0)

请试一试。

private class DownloadFile extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... strings) {
        String fileUrl = strings[0];  
        String fileName = strings[1];  
        String extStorageDirectory = Environment.getExternalStorageDirectory().getPath() + "/test/";
        File folder = new File(extStorageDirectory);
        if (!folder.exists()) {
          folder.mkdirs();
        }
        fileName = extStorageDirectory + fileName;
        File pdfFile = new File(fileName);

        try{
            pdfFile.createNewFile();
        }catch (IOException e){
            e.printStackTrace();
        }
        FileDownloader.downloadFile(fileUrl, pdfFile);
        return null;
    }