V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nnegier
V2EX  ›  Android

Android 遇到了一个问题不知道怎么解决,安装 apk?

  •  
  •   nnegier · 80 天前 · 2212 次点击
    这是一个创建于 80 天前的主题,其中的信息可能已经有所发展或是发生改变。

    折腾了一天一夜未果。首先我要安装的 apk 的路径是下载到公共 Download 目录下的一个文件,所有文件的访问权限都允许了的,file:///storage/emulated/0/Download/timetr-38.apk 。然后通过 FileProvider.getUriForFile 得到 Uri 后发送隐式 Intent 去安装,三星手机安装提示“解析软件包出现问题。”,我的 Android 手机版本是 14 ,首先安装包肯定没有问题,我去文件管理手动安装尝试过,查看 AndroidStudio 控制台日志后发现的:

    Cannot parse package content://com.xuebinduan.tomatotimetracker.fileprovider/root-path/file%3A/storage/emulated/0/Download/timetr-38.apk. Assuming defaults.
    
    stageApk: InstallData{
    mApkInfo=ApkInfo{mUri=content://com.xuebinduan.tomatotimetracker.fileprovider/root-path/file%3A/storage/emulated/0/Download/timetr-36.apk, mSourceFilePath='/file:/storage/emulated/0/Download/timetr-36.apk', mStagedFile='null', mPackageInfo=null}
    , mOriginAppInfo=OriginAppInfo{uid=11153, packageName='com.xuebinduan.tomatotimetracker', AttributionTag='null', isGATest=false, originalUri=null, referrerUri=null, installerPackageName='null', isTrustedSource=false, returnResult=false}
    , mSessionId=-1
    , mySessionId=-1
    , mStagedSessionId=967735638
    , mDeleteApkWhenFinished=true
    , action='android.intent.action.VIEW'}
    

    我个人认为问题要么出在 FileProvider ,要么就是需要 DocumentFile ?但是 DocumentFile 也试过,可能是获取 DocumentFile 的方式不对,发现更有问题。但问题肯定出在 Uri 上。

    我的代码如下:

    AndroidManifest.xml

    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
            <provider
                android:name="androidx.core.content.FileProvider"
                android:authorities="${applicationId}.fileprovider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths" />
            </provider>
    

    file_paths

        <external-path
            name="update_apk" path="Download/"/>
        <root-path
            name="root-path"
            path="." />
    

    安装 apk 的代码

        private void installAPK(String apkFilePath) {
            if (!TextUtils.isEmpty(apkFilePath)) {
                Intent install = new Intent(Intent.ACTION_VIEW);
                install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                install.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true); //表明不是未知来源
                File apkFile = new File(apkFilePath);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri contentUri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", apkFile);
                    install.setDataAndType(contentUri, "application/vnd.android.package-archive");
                } else {
                    install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
                }
                activity.startActivity(install);
            }
        }
    

    build.gradle

    compileSdk 33
    targetSdkVersion 30
    
    第 1 条附言  ·  79 天前
    问题解决了,问题在 file:///storage/emulated/0/Download/timetr-38.apk ,这是个 Uri ,靠,我们要的是/storage/emulated/0/Download/timetr.apk 。

    问题归根是出在没有仔细看网上 copy 的获取 DownloadManager 下载路径的代码,以为它获取的是我要的。就是这行代码:
    ```
    savePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
    ```
    murmur
        1
    murmur  
       80 天前
    new Intent(Intent.ACTION_INSTALL_PACKAGE)

    难道不是要用这个 intent 么
    nnegier
        2
    nnegier  
    OP
       79 天前
    @murmur 我改成你那个了,结果是一样的,装不上,报的异常日志都是一样的。貌似那个安装器的 intent ,Intent.ACTION_INSTALL_PACKAGE 和 Intent.ACTION_VIEW 加 application 的 DataAndType 都能用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2820 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:57 · PVG 17:57 · LAX 02:57 · JFK 05:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.