I am trying to delete file from my External/Removable SD Card. But I got following error:
remove failed: EACCES (Permission denied) : /storage/987F-099F/SDNumber/Voc_112_1.png
I basically need to move file from my removable SD card to my device storage. But I am not able to do that also.
I can fetch all the files from my removable SD card. Here is code for how I am fetching files from removable SD card:
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);
From this when I try to move file from SD card to device external storage it gives me error like this:
rename failed: EXDEV (Cross-device link) : /storage/987F-099F/SDNumber/Voc_112_1.png
Then, I move ahead and trying to copy that file. Copy file works great. But, then after I need to delete that file but it also doesn't work and gives error like this:
remove failed: EACCES (Permission denied) : /storage/987F-099F/SDNumber/Voc_112_1.png
Basically my whole code is like this:
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();
}
I am also trying to delete using content resolver:
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();
}
But none of this function works. That file still appear in my gallery.
Please let me know where I am going wrong and what is missing here. I am also checking runtime permission for Read and Write external storage.
Here is my 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>
Thanks!