如何摆脱异常 "IOException: File '... ... ... .pdf' cannot be written to"
How to get rid of an exception "IOException: File '... ... ... .pdf' cannot be written to"
我想从 link 下载 PDF 文件并在 pdf-reader 中打开它。为此,我指出:
在宣言中:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在classAppAsyncTasks.java:
public static class FileLoadingTask extends AsyncTask<Void, Void, File> {
private static final String LOG_TAG = "FileLoadingTask";
public FileLoadingTask(final Context cxt,
final JSONOrder order,
final FileLoadingListener listener) {
}
@Override
protected void onPreExecute() {
super.onPreExecute();
createPdLoading(pdLoading);
this.url = mOrder.getPreviewlink();
this.extStorageDirectory = Environment.getExternalStorageDirectory().toString();
this.folder = new File(extStorageDirectory, "Download");
this.folder.mkdir();
this.fileName = mOrder.getOrdercode() + ".pdf";
this.file = new File(this.folder, this.fileName);
fileLoadingListener.onBegin();
}
@Override
protected File doInBackground(Void... params) {
try {
FileUtils.copyURLToFile(new URL(url), file);
} catch (IOException e) {
throwable = e;
}
return file;
}
@Override
protected void onPostExecute(File file) {
super.onPostExecute(file);
pdLoading.dismiss();
fileLoadingListener.onEnd(file);
if (throwable != null) {
fileLoadingListener.onFailure(throwable);
} else {
fileLoadingListener.onSuccess();
}
}
public interface FileLoadingListener {
void onBegin();
void onSuccess();
void onFailure(Throwable cause);
void onEnd(File file);
}
}
但是,不会将文件写入内部媒体 - 抛出异常:
IOException: File 'storage/emulated/0/Download/...pdf' cannot be written to
如何摆脱这个异常?
这个解决方案是可行的。我在代码中包含了检查外部存储器写入权限的说明。正如 Tejas Pandya and Dhaval Patel 所建议的那样。
为此,我的onPreExecute()方法添加如下:
@Override
protected void onPreExecute() {
super.onPreExecute();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
while (checkPermissions()) {
}
}
createPdLoading(pdLoading);
... ... ... ... ...
}
方法 checkPermissions() returns 如果没有将文件写入外部存储器的权限,则值 "true",如果有 - "false",如果有。
@RequiresApi(api = Build.VERSION_CODES.M)
private boolean checkPermissions() {
switch (App.getCurFragContext().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
case PackageManager.PERMISSION_DENIED:
fileLoadingListener.requestPermissions();
return true;
default:
return false;
}
}
我向 FileLoadingListener 接口添加了一个 requestPermissions() 方法。它从 checkPermissions() 方法调用并具有以下实现:
@Override
public void requestPermissions() {
ActivityCompat.requestPermissions(
(Activity) mActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
App.MY_PERMISSIONS_REQUEST_WRITE);
}
我想从 link 下载 PDF 文件并在 pdf-reader 中打开它。为此,我指出:
在宣言中:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在classAppAsyncTasks.java:
public static class FileLoadingTask extends AsyncTask<Void, Void, File> { private static final String LOG_TAG = "FileLoadingTask"; public FileLoadingTask(final Context cxt, final JSONOrder order, final FileLoadingListener listener) { } @Override protected void onPreExecute() { super.onPreExecute(); createPdLoading(pdLoading); this.url = mOrder.getPreviewlink(); this.extStorageDirectory = Environment.getExternalStorageDirectory().toString(); this.folder = new File(extStorageDirectory, "Download"); this.folder.mkdir(); this.fileName = mOrder.getOrdercode() + ".pdf"; this.file = new File(this.folder, this.fileName); fileLoadingListener.onBegin(); } @Override protected File doInBackground(Void... params) { try { FileUtils.copyURLToFile(new URL(url), file); } catch (IOException e) { throwable = e; } return file; } @Override protected void onPostExecute(File file) { super.onPostExecute(file); pdLoading.dismiss(); fileLoadingListener.onEnd(file); if (throwable != null) { fileLoadingListener.onFailure(throwable); } else { fileLoadingListener.onSuccess(); } } public interface FileLoadingListener { void onBegin(); void onSuccess(); void onFailure(Throwable cause); void onEnd(File file); } }
但是,不会将文件写入内部媒体 - 抛出异常:
IOException: File 'storage/emulated/0/Download/...pdf' cannot be written to
如何摆脱这个异常?
这个解决方案是可行的。我在代码中包含了检查外部存储器写入权限的说明。正如 Tejas Pandya and Dhaval Patel 所建议的那样。
为此,我的onPreExecute()方法添加如下:
@Override
protected void onPreExecute() {
super.onPreExecute();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
while (checkPermissions()) {
}
}
createPdLoading(pdLoading);
... ... ... ... ...
}
方法 checkPermissions() returns 如果没有将文件写入外部存储器的权限,则值 "true",如果有 - "false",如果有。
@RequiresApi(api = Build.VERSION_CODES.M)
private boolean checkPermissions() {
switch (App.getCurFragContext().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
case PackageManager.PERMISSION_DENIED:
fileLoadingListener.requestPermissions();
return true;
default:
return false;
}
}
我向 FileLoadingListener 接口添加了一个 requestPermissions() 方法。它从 checkPermissions() 方法调用并具有以下实现:
@Override
public void requestPermissions() {
ActivityCompat.requestPermissions(
(Activity) mActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
App.MY_PERMISSIONS_REQUEST_WRITE);
}