最近自己写插件时,总是发现packageManager.getPackageArchiveInfo 返回为null,但是自己权限都打开,加载路径也是正确的,到底是为什么呢?
packageInfo =packageManager.getPackageArchiveInfo(path,PackageManager.GET_ACTIVITIES );
原来path写法有问题,之前的写法:
路径为:/storage/emulated/0/DCIM/plugin-debug.apk
Environment.getExternalStorageDirectory()+"/DCIM/plugin-debug.apk"
可是上面的写法在Android9.0 之后就废弃了(为了提高用户隐私,不建议直接访问共享/外部存储设备),并且不再返回可访问的文件。
那怎么写呢,官方推荐使用:
getExternalFilesDir(null).getPath()+"/plugin-debug.apk";
修改之后,返回值就不为null了。
路径为:/storage/emulated/0/Android/data/com.example.insert/files/plugin-debug.apk
String path=context.getExternalFilesDir(null).getAbsolutePath();
File file=new File(path);
//输出:path:/storage/emulated/0/Android/data/packageName/filesString path2=context.getExternalFilesDir("UniApp").getAbsolutePath();
File file2=new File(path2);
//path:/storage/emulated/0/Android/data/packageName/files/UniApp
//如uniapp文件夹没有,则会自动创建String path3=context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
File file3=new File(path3);
//path:/storage/emulated/0/Android/data/packageName/files/DownloadString path4=context.getExternalFilesDir("").getAbsolutePath()+"/hhhhhh";
File file4=new File(path4);
//path:storage/emulated/0/Android/data/packageName/files/hhhhhh
//如果没有“hhhhhh”文件夹,则file4.exists()==false;