同样获取权限代码一个项目
本地打包
云打包
这样就会造成我无法使用红框里面的数据
`"distribute" : {
"android" : {
"permissions" : [
"<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>",
"<uses-permission android:name=\"android.permission.VIBRATE\"/>",
"<uses-permission android:name=\"android.permission.READ_LOGS\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>",
"<uses-feature android:name=\"android.hardware.camera.autofocus\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.CAMERA\"/>",
"<uses-permission android:name=\"android.permission.GET_ACCOUNTS\"/>",
"<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>",
"<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>",
"<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",
"<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>",
"<uses-feature android:name=\"android.hardware.camera\"/>",
"<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>",
"<uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\" />",
"<uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" />"
]
},`
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:allowClearUserData="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true">
<activity
android:name="io.dcloud.PandoraEntry"
android:configChanges="orientation|keyboardHidden|keyboard|navigation"
android:label="@string/app_name"
android:launchMode="singleTask"
android:hardwareAccelerated="true"
android:theme="@style/TranslucentTheme"
android:screenOrientation="user"
android:exported="true"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="io.dcloud.PandoraEntryActivity"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale|keyboard|smallestScreenSize|screenLayout|screenSize|uiMode"
android:hardwareAccelerated="true"
android:permission="com.miui.securitycenter.permission.AppPermissionsEditor"
android:screenOrientation="user"
android:theme="@style/DCloudTheme"
android:exported="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme=" " />
</intent-filter>
</activity>
<meta-data
android:name="dcloud_appkey"
android:value="99a20518bf08ce858bde2da98fa33f1e" />
</application>
</manifest>
export const openFileManager = ()=> {
return new Promise(async (resolve, reject) => {
try {
await requestPermission();
const main = plus.android.runtimeMainActivity();
const Intent = plus.android.importClass('android.content.Intent');
const intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType('*/*');
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
const REQUEST_CODE = 1;
main.onActivityResult = (requestCode, code, data) => {
const Activity = plus.android.importClass('android.app.Activity');
plus.android.importClass('android.net.Uri');
let filePathList = [];
if (code === Activity.RESULT_OK && requestCode === REQUEST_CODE) {
let Uri = data.getData();
if (!Uri) {
let ClipData = plus.android.importClass('android.content.ClipData');
let clipData = new ClipData();
clipData = data.getClipData();
if (clipData) {
const count = clipData.getItemCount();
for (let i = 0; i < count; i++) {
const item = clipData.getItemAt(i);
const uri = item.getUri();
const filePath = getFilePath(main, uri);
filePathList.push(filePath);
}
}
} else {
filePathList.push(getFilePath(main, Uri));
}
}
resolve(filePathList);
};
main.startActivityForResult(intent, REQUEST_CODE);
} catch (e) {
reject(e);
}
});
}
function getFilePath(main, Uri) {
const Build = plus.android.importClass('android.os.Build');
const DocumentsContract = plus.android.importClass('android.provider.DocumentsContract');
const IS_DOUMENT_TYPE = DocumentsContract.isDocumentUri(main, Uri);
const scheme = Uri.getScheme();
const IS_KITKAT = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (IS_KITKAT) {
if (IS_DOUMENT_TYPE) {
const docId = DocumentsContract.getDocumentId(Uri);
const [type, id] = docId.split(':');
const authority = Uri.getAuthority();
const AUTHORITY_DOCUMENT_TYPE = {
MEDIA: 'com.android.providers.media.documents',
DOWNLOAD: 'com.android.providers.downloads.documents',
EXTERNAL: 'com.android.externalstorage.documents',
};
const authorityFn = {
[AUTHORITY_DOCUMENT_TYPE.DOWNLOAD]: getDownloadDocument,
[AUTHORITY_DOCUMENT_TYPE.MEDIA]: getMediaDocument,
[AUTHORITY_DOCUMENT_TYPE.EXTERNAL]: getExternalDocument,
};
return authorityFn[authority]?.({ main, Uri, docId, type, id });
} else if (scheme === 'content') {
return getDataColumn(main, Uri);
} else if (scheme === 'file') {
return Uri.getPath();
}
} else {
const MediaStore = plus.android.importClass('android.provider.MediaStore');
const columns = [MediaStore.Images.Media.DATA];
const cursor = contentResolver.query(Uri, columns, null, null, null);
if (cursor !== null && cursor.moveToFirst()) {
const column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
const filePath = cursor.getString(column_index);
cursor.close();
return filePath;
}
}
}
function getExternalDocument(options) {
const { type, id } = options;
const Environment = plus.android.importClass('android.os.Environment');
let path = '';
if (type === 'primary') {
path = `${Environment.getExternalStorageDirectory()}/${id}`;
} else {
const System = plus.android.importClass('java.lang.System');
path = `${System.getenv('SECONDARY_STORAGE')}/${id}`;
}
return path;
}
function getMediaDocument(options) {
const { main, id, type } = options;
const MediaStore = plus.android.importClass('android.provider.MediaStore');
const MEDIA_TYPE = {
image: MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
video: MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
audio: MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
document: MediaStore.Files.getContentUri('external'),
};
const uri = MEDIA_TYPE[type] || null;
return getDataColumn(main, uri, '_id=?', [id]);
}
function getDownloadDocument(options) {
const { main, Uri, id } = options;
if (id) {
return id;
}
const ContentUris = plus.android.importClass('android.content.ContentUris');
const originId = Uri.parse('content://downloads/public_downloads');
const newId = ContentUris.parseId(Uri);
const uri = ContentUris.withAppendedId(originId, newId);
return getDataColumn(main, uri);
}
function getDataColumn(context, uri, selection = null, selectionArgs = null) {
plus.android.importClass(context.getContentResolver());
const _columns = ['_data'];
const Cursor = context.getContentResolver().query(uri, _columns, selection, selectionArgs, null);
Cursor && plus.android.importClass(Cursor);
let filePath = '';
if (Cursor !== null && Cursor.moveToFirst()) {
const columnIndex = Cursor.getColumnIndexOrThrow(_columns[0]);
filePath = Cursor.getString(columnIndex);
Cursor.close();
}
return filePath;
}
const PERMISSION_TYPE = {
PERMISSION_GRANTED: 0,
PERMISSION_DENIED: 1,
PERMISSION_DENIED_FOREVER: 2,
};
function requestPermission() {
return new Promise((resolve, reject) => {
plus.android.requestPermissions(
['android.permission.WRITE_EXTERNAL_STORAGE'],
(e) => {
if (e.deniedAlways.length > 0) {
reject({
code: PERMISSION_TYPE.PERMISSION_DENIED_FOREVER,
msg: '未授权且不再询问',
});
}
if (e.deniedPresent.length > 0) {
reject({
code: PERMISSION_TYPE.PERMISSION_DENIED,
msg: '未授权',
});
}
if (e.granted.length > 0) {
resolve({
code: PERMISSION_TYPE.PERMISSION_GRANTED,
msg: '已授权',
});
}
},
(e) => {
reject(e);
}
);
});
}