Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android FileProvider delete file

Tags:

android

Am working in a photo gallery app, due to the recent nougat update i couldn't delete file from gallery. I found out that I have to use Fileprovider for file access, I tried below code but it says

04-25 12:52:03.031 3204-4133/com.zo.tns1 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
                                                       Process: com.zo.tns1, PID: 3204
                                                       java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                           at android.os.AsyncTask$3.done(AsyncTask.java:325)
                                                           at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                           at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                           at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                           at java.lang.Thread.run(Thread.java:761)
                                                        Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.io.File android.support.v4.content.FileProvider$PathStrategy.getFileForUri(android.net.Uri)' on a null object reference
                                                           at android.support.v4.content.FileProvider.delete(FileProvider.java:520)
                                                           at com.zo.tns1.tasks.DeleteMediaTask.doInBackground(DeleteMediaTask.java:117)
                                                           at com.zo.tns1.tasks.DeleteMediaTask.doInBackground(DeleteMediaTask.java:39)
                                                           at android.os.AsyncTask$2.call(AsyncTask.java:305)
                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                           at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                           at java.lang.Thread.run(Thread.java:761) 

Manifest

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.zo.tns1.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/fileaccess_paths"/>
    </provider>

fileaccess_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="all_files" path="." />

Java Code

File f = new File(mediaList.get(i).getMediaPath());    
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                FileProvider provider = new FileProvider();
                Uri deleteFileUri = FileProvider.getUriForFile(context.getApplicationContext(), "com.zo.tns1.fileprovider", f);
                context.grantUriPermission(context.getApplicationContext().getPackageName(), deleteFileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                provider.delete(deleteFileUri, null, null);
                //context.getContentResolver().delete(deleteFileUri, null, null);
            } else {
                boolean b = f.delete();
                Log.d(TAG, "File delete -> " + b);
            }

FilePath

/storage/emulated/0/DCIM/Camera/IMG_20170421_112804_1.jpg
like image 342
Arun Shankar Avatar asked Feb 05 '23 15:02

Arun Shankar


1 Answers

You can't just use the FileProvider constructor like that, you need to go via your context's content resolver. That will give you the provider you've specified in the manifest. Something like

context.getContentResolver().delete(deleteFileUri, null, null);
like image 101
GertG Avatar answered Feb 07 '23 11:02

GertG