【发布时间】:2018-03-20 06:41:39
【问题描述】:
我正在尝试从我的外部/可移动 SD 卡中删除文件。但我收到以下错误:
remove failed: EACCES (Permission denied) : /storage/987F-099F/SDNumber/Voc_112_1.png
我基本上需要将文件从我的可移动 SD 卡移动到我的设备存储中。 但我也做不到。
我可以从我的可移动 SD 卡中获取所有文件。 这是我如何从可移动 SD 卡中获取文件的代码:
final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME};
final String orderBy = MediaStore.Images.Media.DISPLAY_NAME;
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
当我尝试将文件从 SD 卡移动到设备外部存储时,它给了我这样的错误:
rename failed: EXDEV (Cross-device link) : /storage/987F-099F/SDNumber/Voc_112_1.png
然后,我继续尝试复制该文件。复制文件效果很好。但是,在我需要删除该文件但它也不起作用并给出如下错误之后:
remove failed: EACCES (Permission denied) : /storage/987F-099F/SDNumber/Voc_112_1.png
基本上我的整个代码是这样的:
public static void moveFile(Context context, File srcFile, File destFile) {
boolean rename = srcFile.renameTo(destFile);
if (!rename) {
copyFile(srcFile.getAbsolutePath(), destFile.getAbsolutePath());
boolean deleted = deleteFile(srcFile.getAbsolutePath());
if (!deleted) {
deleted = delete(context, srcFile);
Log.e("moveFile", "deleted : " + deleted);
}
}
}
public static boolean copyFile(String sourceFilePath, String destFilePath) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(sourceFilePath);
} catch (FileNotFoundException e) {
throw new RuntimeException("FileNotFoundException occurred. ", e);
}
return writeFile(destFilePath, inputStream);
}
public static boolean deleteFile(String path) {
if (path == null || path.trim().length() == 0) {
return true;
}
File file = new File(path);
if (!file.exists()) {
return true;
}
if (file.isFile()) {
return file.delete();
}
if (!file.isDirectory()) {
return false;
}
for (File f : file.listFiles()) {
if (f.isFile()) {
f.delete();
} else if (f.isDirectory()) {
deleteFile(f.getAbsolutePath());
}
}
return file.delete();
}
我也在尝试使用内容解析器删除:
public static boolean delete(final Context context, final File file) {
final String where = MediaStore.MediaColumns.DATA + "=?";
final String[] selectionArgs = new String[] {
file.getAbsolutePath()
};
final ContentResolver contentResolver = context.getContentResolver();
final Uri filesUri = MediaStore.Files.getContentUri("external");
contentResolver.delete(filesUri, where, selectionArgs);
if (file.exists()) {
contentResolver.delete(filesUri, where, selectionArgs);
}
return !file.exists();
}
但是这个功能都不起作用。该文件仍然出现在我的画廊中。
请告诉我哪里出了问题以及这里缺少什么。 我还在检查读写外部存储的运行时权限。
这是我的 Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pix.tours.pixtours">
<uses-permission android:name="android.permission.CALL_PHONE" />
<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" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.android.vending.BILLING" />
<application
android:name=".GlobalApplication"
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@drawable/logo"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activities.MainActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".activities.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
谢谢!
【问题讨论】:
-
显示你的清单文件
-
@krishank Tripathi 请检查更新的问题。
-
Android 是 Linux。反过来,它是 Unix。确保您(您的应用程序)对该特定文件具有写入 (w) 权限。
-
如何向我的可移动 SD 卡添加写入权限?
-
Here is code for how I am fetching files from removable SD card:该代码永远不会为您提供您提到的路径。
标签: android storage android-permissions android-sdcard permission-denied