gpt4 book ai didi

Android FileProvider删除文件

转载 作者:太空狗 更新时间:2023-10-29 16:12:07 28 4
gpt4 key购买 nike

我在照片库应用程序中工作,由于最近的牛轧糖更新,我无法从图库中删除文件。我发现我必须使用 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) 

list

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

文件访问路径.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

最佳答案

您不能像那样只使用 FileProvider 构造函数,您需要通过上下文的内容解析器。这将为您提供您在 list 中指定的提供者。有点像

context.getContentResolver().delete(deleteFileUri, null, null);

关于Android FileProvider删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43536531/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com