ava.io.IOException:open failed:EACCES(Permission denied)

时间:2016-07-04 06:46:58

标签: android android-internal-storage

我正在使用ThinDownloadManager库从url下载视频。需要将视频设为私有,因此我希望使用internal storage来保存下载视频以使其保密。在上面使用的库我们提供的两件事,首先是url,第二是存储视频文件的路径。当我给出内部路径时,它会产生异常并调用视频onDownloadFailed方法。

以下是我的代码

public void startVideoDownloading() {
        //Show downloading in notification bar

        final NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle("Video downloading")
                .setContentText("Download in progress")
                .setSmallIcon(R.drawable.ic_notification);

        ThinDownloadManager downloadManager = new ThinDownloadManager();
        Uri downloadUri = Uri.parse(videoId);
        File fileDir = createDirectory();
        Uri destinationUri = Uri.parse(fileDir + uniqueId);
        DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
                .setDestinationURI(destinationUri).setPriority(DownloadRequest.Priority.HIGH)
                .setDownloadListener(new DownloadStatusListener() {
                    @Override
                    public void onDownloadComplete(int id) {
                        Toast.makeText(Player.this, "Download Completed", Toast.LENGTH_SHORT).show();

                        mBuilder.setContentText(" Video download completed")
                                .setProgress(0, 0, false);
                        mNotifyManager.notify(id, mBuilder.build());

                    }

                    @Override
                    public void onDownloadFailed(int id, int errorCode, String errorMessage) {
                        Toast.makeText(Player.this, "Download Failed", Toast.LENGTH_SHORT).show();
                        mBuilder.setContentTitle("Failed");
                        mBuilder.setContentText("Downloading failed")
                                .setProgress(0, 0, false);
                        mNotifyManager.notify(id, mBuilder.build());

                    }

                    @Override
                    public void onProgress(int id, long totalBytes, long downloadedBytes, int progress) {
                        donutProgress.setProgress(progress);

                        mBuilder.setProgress(100, progress, false);

                        mNotifyManager.notify(id, mBuilder.build());

                    }
                });
        downloadManager.add(downloadRequest);
    }

    public File createDirectory() {
        File folder = new File(Environment.getDataDirectory() + "/+" + "downloadVideo/");
        if (!folder.exists()) {
            folder.mkdir();
            Log.d("TAG","Directory created");
        }else {
            Log.d("TAG","Directory exists");
        }
        return folder;

    }

下面是我的logcat错误

java.io.IOException: open failed: EACCES (Permission denied)
at java.io.File.createNewFile(File.java:946)
at com.thin.downloadmanager.DownloadDispatcher.transferData(DownloadDispatcher.java:213)
at com.thin.downloadmanager.DownloadDispatcher.executeDownload(DownloadDispatcher.java:142)
at com.thin.downloadmanager.DownloadDispatcher.run(DownloadDispatcher.java:81)
0Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at java.io.File.createNewFile(File.java:939)

当我提供外部存储路径工作正常。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您很可能需要在清单中添加以下行(详见此处:https://developer.android.com/training/basics/data-storage/files.html

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

此外,如果您的应用程序的目标是Android 6.0或更高版本,则需要在运行时向用户请求此权限 - 详细信息请参阅:https://developer.android.com/training/permissions/requesting.html