文章目录
一、报错信息
二、解决方案
一、报错信息
Android 分区存储 , 将 图片文件 保存到 Movies 目录下报错 :
2021-05-18 14:31:50.691 1341-5448/com.google.android.providers.media.module E/DatabaseUtils: Writing exception to parcel java.lang.IllegalArgumentException: Primary directory Movies not allowed for content://media/external/images/media; allowed directories are [DCIM, Pictures] at com.android.providers.media.MediaProvider.ensureFileColumns(MediaProvider.java:2682) at com.android.providers.media.MediaProvider.ensureUniqueFileColumns(MediaProvider.java:2347) at com.android.providers.media.MediaProvider.insertFile(MediaProvider.java:2957) at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:3361) at com.android.providers.media.MediaProvider.insert(MediaProvider.java:3208) at android.content.ContentProvider$Transport.insert(ContentProvider.java:336) at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:167) at android.os.Binder.execTransactInternal(Binder.java:1154) at android.os.Binder.execTransact(Binder.java:1123) 2021-05-18 14:31:50.695 6361-6361/kim.hsl.file E/AndroidRuntime: FATAL EXCEPTION: main Process: kim.hsl.file, PID: 6361 java.lang.RuntimeException: Unable to start activity ComponentInfo{kim.hsl.file/kim.hsl.file.MainActivity}: java.lang.IllegalArgumentException: Primary directory Movies not allowed for content://media/external/images/media; allowed directories are [DCIM, Pictures] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.IllegalArgumentException: Primary directory Movies not allowed for content://media/external/images/media; allowed directories are [DCIM, Pictures] at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142) at android.content.ContentProviderProxy.insert(ContentProviderNative.java:549) at android.content.ContentResolver.insert(ContentResolver.java:2149) at android.content.ContentResolver.insert(ContentResolver.java:2111) at kim.hsl.file.MainActivity.createImageFile(MainActivity.kt:116) at kim.hsl.file.MainActivity.doSomethingWithPermissions(MainActivity.kt:41) at kim.hsl.file.MainActivity.onCreate(MainActivity.kt:24) at android.app.Activity.performCreate(Activity.java:8000) at android.app.Activity.performCreate(Activity.java:7984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 2021-05-18 14:31:50.797 321-321/? E/Layer: [Surface(name=Task=44)/@0x35761af - animation-leash#0] No local sync point found 2021-05-18 14:31:50.797 321-321/? E/Layer: [Surface(name=Task=1)/@0xe9c7852 - animation-leash#0] No local sync point found
二、解决方案
在 Android 11 1111 之后 , 不能使用 File 进行文件操作 , 需要使用 MediaStore 进行文件操作 ,
MediaStore 的如下内部类 Files , Images , Downloads , Audio , Video , 负责相应目录的文件操作 , 分别对应外置存储中的 Document , Pictures , Download , Music , Movies 目录 ;
对应格式的文件 , 只能放在特定的目录中 ;
文件存储类型限制 : 文件一旦放错位置 , 就会抛异常 ;
专门存储 图片文件 的目录 Pictures , 只能存放图片 , 不能存放其它类型文件 ,
专门存储 视频文件 的目录 Movies , 只能存视频文件 ;
专门存储音频文件的目录 Music , 只能存储音频文件 ;
专门存储文档文件的目录 Document , 只能存储文档相关文件 ;
Download 目录可以放置任何类型的文件 ;