在 Android 12 上以编程方式安装 APK - “访问文件的权限:/storage/emulated/0/xxxx.apk 被拒绝”

问题描述 投票:0回答:1

使用Android Studio,Java语言,我编写了一个应用程序来安装APK为/eduphone/eduphone.apk的包 在 Android 12 上,我在 Logcat 上收到以下错误: 访问文件的权限:/storage/emulated/0/Download/eduphone/eduphone.apk 被拒绝

我尝试了 Stak Overflow 中提出的几种解决方案,但没有一个有效。

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
    <uses-permission android:name="com.termux.permission.RUN_COMMAND"/>



    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:exported="true"
        android:theme="@style/Theme.Test4"
        tools:targetApi="31">
        <activity
            android:name="eu.eduphone.install.MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:permission="android.permission.MANAGE_DOCUMENTS"
        android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
    <queries>
        <package android:name="com.termux" />
        <package android:name="eu.edduphone" />
    </queries>
</manifest>`

这是provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="." />
</paths>

这是获取权限的Java代码:

public void getdroits () {

        if (ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{READ_EXTERNAL_STORAGE}, 1);
        }

        if (ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{WRITE_EXTERNAL_STORAGE}, 1);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if(!getPackageManager().canRequestPackageInstalls()){
                startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
                        .setData(Uri.parse(String.format("package:%s", getPackageName()))), 1);
            }
        }
    }

这里是 APK 安装的 Java 代码。 传递给此代码的参数是 String PATH = "/sdcard/Download/eduphone/eduphone.apk"

  private  void instApp(String PATH){
        Context context = getActivity().getBaseContext();
        File file = new File(PATH);

        if(file.exists()) {

            Uri  uri ;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
            } else {
                uri = Uri.fromFile(file);
            }

            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE, uri);

            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            try {
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
                Log.e("TAG", "Error in opening the file!");
            }
        }else{
            Toast.makeText(context,"APK "+PATH+" not found",Toast.LENGTH_LONG).show();
        }
    }

感谢您的帮助! 蒂埃里

java android installation package permission-denied
1个回答
0
投票

您无权访问

Downloads/
中的文件,您自己从应用程序放置在那里的文件除外。

您可以使用

ACTION_OPEN_DOCUMENT
/
ActivityResultContracts.OpenDocument
让用户选择 APK,而不是考虑文件系统路径。然后,您可以将您获得的
Uri
传递回您的
ACTION_INSTALL_PACKAGE
Intent
。您不需要文件系统权限,也不需要
FileProvider
采用这种方法。

© www.soinside.com 2019 - 2024. All rights reserved.