[英]java.io.FileNotFoundException open failed: EPERM (Operation not permitted)
修改 build.gradle 后: compileSdkVersion 28 to 32 targetSdkVersion 28 to 32 我無法從改造響應中寫入本地文件。 我得到錯誤:
java.io.FileNotFoundException: /storage/emulated/0/DontEat/rangt_00001.jpg: open failed: EPERM (Operation not permitted)
AndroidManifest.xml 包含:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我的代碼是:
public static void downloadFile(SyncCalls.CallbacksDownload callbacks, final String type, final File file, final Boolean last) {
final WeakReference<SyncCalls.CallbacksDownload> callbacksWeakReference = new WeakReference<>(callbacks);
SyncService service = SyncService.retrofit.create(SyncService.class);
String filename = file.toString().split("/")[5];
Call<ResponseBody> call = service.downloadFile(filename);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
try {
OutputStream out = null;
InputStream in = null;
ResponseBody body = response.body();
try {
if (body != null) {
in = body.byteStream();
out = new FileOutputStream(file);
byte[] fileReader = new byte[4096];
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
while (true) {
int read = in.read(fileReader);
if (read == -1) {
break;
}
out.write(fileReader, 0, read);
}
out.flush();
}
} catch (IOException e) {
if (BuildConfig.DEBUG) Log.v(Consts.TAG, e.toString());
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
} catch (IOException e) {
if (BuildConfig.DEBUG) Log.v(Consts.TAG, e.toString());
}
if (BuildConfig.DEBUG) Log.v(Consts.TAG, "download " + file.toString());
if (callbacksWeakReference.get() != null)
callbacksWeakReference.get().onResponseSync(type, "down", last);
}
權限通過以下方式驗證:
// Storage Permissions variables
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static final String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
//permission method.
public static void verifyStoragePermissions(Activity activity) {
// Check if we have read or write permission
int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int readPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
if (writePermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
請問是我的代碼錯誤還是權限錯誤?
由於我的英語非常有限,我沒有發展更多。 (這是我多年前制作的一個應用程序,僅供個人使用,家庭,但我無法擴展它,因為這個錯誤出現在新的 android 版本中,而到目前為止它運行良好)
編輯:實際上,我認為我將問題顛倒過來,我最好將應用程序的內部數據目錄與 getFilesDir() 一起使用,如果我理解正確,我將不再有權限問題。
在AndroidManifest.xml
中添加該行,在應用程序標簽內。希望它會幫助你。
android:requestLegacyExternalStorage=”true”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.