删除失败:EACCES(权限被拒绝) - 从外部SD卡删除文件

时间:2018-03-20 06:41:40

标签: android storage android-permissions android-sdcard permission-denied

我正在尝试从外部/可移动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>

谢谢!

2 个答案:

答案 0 :(得分:0)

您是否在runtime申请了存储空间许可?即使您在清单中添加了uses-permission,仍然需要让用户在运行时授予权限。这是代码:

/ Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

在此处详细了解运行时权限请求:https://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:0)

  

重命名失败:EXDEV(跨设备链接):/ storage / 987F-099F / SNGumber / Voc_112_1.png

自Android 4.4 +。

以来,可移动SD卡是只读的(使用文件方案)

能够编写使用存储访问框架。