uniapp offline packaging to add file read and write permissions

问题:The project uses file storage data requirements. The read and write permissions have been added to the manifest.json in the project. There is no problem with real machine operation and cloud packaging. The data in the file can be read, but offline packaging and read data Just empty.

Android Studio is used for uniapp offline packaging. The read and write permissions of files are used in the project. You need to add read and write permissions in the packaging project UniPlugin-Hello-AS.

Specific operation:
Modify UniPlugin-Hello-AS/app/src/main/AndroidManifest.xmlthe file, and all permission configurations are in AndroidManifestit.

1. Add read and write permissions

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2. Add in application (very important)

android:requestLegacyExternalStorage="true"

注意:Only adding read and write permissions will not take effect, you must add the content of step 2 (at the beginning I only added the permission of the first step, the content of the read file is empty, after adding the second step, you can read the content of the file content, do not know why).

AndroidManifest.xml file content:

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

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true" />
        <!-- 读写权限 -->
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:name="io.dcloud.application.DCloudApplication"
        android:allowBackup="true"
        android:allowClearUserData="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:largeHeap="true"
        android:supportsRtl="true">
         <meta-data
            android:name="dcloud_appkey"
            android:value="替换为自己申请的Appkey" />
    </application>

Attached are the default permissions added by uniapp cloud packaging, and the cloud packaging permission configuration of the Android platform :

<uses-permission android:name="android.permission.INTERNET" />  
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
<uses-permission android:name="android.permission.READ_PHONE_STATE" />  
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
<uses-permission android:name="com.asus.msa.SupplementaryDID.ACCESS" />  
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE" />  
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />  
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

Reference blog post: Android 10 dynamically applies for read and write permissions

Guess you like

Origin blog.csdn.net/weixin_41767649/article/details/124039298