2

我正在使用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)

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

4

1 回答 1

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

于 2016-07-04T06:52:52.733 回答