Android 高版本 packageManager.getPackageArchiveInfo 总是返回null

最近自己写插件时,总是发现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

getExternalFilesDir()内部参数

  1. 该方法用于获得外部存储,地址为/storage/emulated/0/Android/data/packageName/files
  2. 该方法可传一个String类型的参数,用于在父路径下创建子文件夹,没有该文件夹会自动创建
  3. 使用方法:
    String path=context.getExternalFilesDir(null).getAbsolutePath();
    File file=new File(path);
    //输出:path:/storage/emulated/0/Android/data/packageName/files
    
    String 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/Download
    
    
    String path4=context.getExternalFilesDir("").getAbsolutePath()+"/hhhhhh";
    File file4=new File(path4);
    //path:storage/emulated/0/Android/data/packageName/files/hhhhhh
    //如果没有“hhhhhh”文件夹,则file4.exists()==false;
    

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以通过将U盘中的db.apk拷贝到系统的Download目录下,然后再通过getPackageArchiveInfo获取Download目录下的db.apk版本信息,以避免在卸载U盘时无法成功卸载的问题。具体实现步骤如下: 1. 在Look.apk中监听ACTION_MEDIA_MOUNTED广播,当检测到U盘插入后,获取U盘的挂载路径; 2. 在U盘挂载路径下查找db.apk文件,如果找到则拷贝到系统的Download目录下; 3. 获取Download目录下的db.apk版本信息,与系统已安装的apk版本信息进行比较,如果需要更新则进行更新操作; 4. 在U盘卸载时,删除Download目录下的db.apk文件。 以下是示例代码: ```java public class LookApk extends BroadcastReceiver { private static final String U_DISK_PATH = "/storage/9016-4EF8"; private static final String APK_NAME = "db.apk"; private static final String DOWNLOAD_DIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(); @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { // 获取U盘挂载路径 String uDiskPath = intent.getData().getPath(); if (uDiskPath.equals(U_DISK_PATH)) { // 在U盘路径下查找db.apk文件 String apkPath = uDiskPath + File.separator + APK_NAME; File apkFile = new File(apkPath); if (apkFile.exists()) { // 将db.apk拷贝到Download目录下 String downloadPath = DOWNLOAD_DIR + File.separator + APK_NAME; try { FileUtils.copyFile(apkFile, new File(downloadPath)); } catch (IOException e) { e.printStackTrace(); } // 获取Download目录下db.apk的版本信息 PackageManager pm = context.getPackageManager(); PackageInfo apkInfo = pm.getPackageArchiveInfo(downloadPath, PackageManager.GET_ACTIVITIES); if (apkInfo != null && apkInfo.packageName.equals("com.example.db") && apkInfo.versionCode > pm.getPackageInfo("com.example.db", 0).versionCode) { // 需要更新db.apk,进行更新操作 Uri uri = Uri.fromFile(new File(downloadPath)); Intent installIntent = new Intent(Intent.ACTION_VIEW); installIntent.setDataAndType(uri, "application/vnd.android.package-archive"); installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(installIntent); } } } } else if (action.equals(Intent.ACTION_MEDIA_EJECT)) { // U盘卸载时,删除Download目录下的db.apk文件 String downloadPath = DOWNLOAD_DIR + File.separator + APK_NAME; File downloadFile = new File(downloadPath); if (downloadFile.exists()) { downloadFile.delete(); } } } } ``` 需要注意的是,上述代码中的U盘路径和APK名称需要根据实际情况进行修改,特别是U盘路径可能因设备而有所不同。此外,需要在AndroidManifest.xml中声明相关权限和广播接收器,具体可参考以下示例代码: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <receiver android:name=".LookApk" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MEDIA_MOUNTED" /> <action android:name="android.intent.action.MEDIA_EJECT" /> <data android:scheme="file" /> </intent-filter> </receiver> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaowang_lj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值