【问题标题】:remove failed: EACCES (Permission denied) - Deleting a file from External SD Card删除失败:EACCES(权限被拒绝) - 从外部 SD 卡中删除文件
【发布时间】: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


【解决方案1】:

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

自 Android 4.4+ 起,可移动 SD 卡为只读(使用文件方案)。

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

【讨论】:

  • 您好,谢谢您的回答。你能告诉我更多关于“存储访问框架”的信息吗?我不知道它的用途。
【解决方案2】:

您是否在runtime 申请存储权限?即使您在清单中添加使用权限,您仍然需要在运行时要求用户授予权限。这是代码:

/ 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

【讨论】:

  • 是的,我也在做同样的事情
猜你喜欢
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多