Android FileProvider删除文件

时间:2017-04-21 07:30:17

标签: android

我在照片库应用中工作,由于最近的牛轧糖更新,我无法从图库中删除文件。 我发现我必须使用Fileprovider进行文件访问,我尝试了下面的代码,但它说

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) 

清单

<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代码

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);
            }

文件路径

/storage/emulated/0/DCIM/Camera/IMG_20170421_112804_1.jpg

1 个答案:

答案 0 :(得分:6)

您不能像这样使用FileProvider构造函数,您需要通过上下文的内容解析器。这将为您提供清单中指定的提供商。像

这样的东西
context.getContentResolver().delete(deleteFileUri, null, null);