我是纸飞机
我是纸飞机
  • 发布:2023-12-28 14:53
  • 更新:2023-12-29 04:51
  • 阅读:295

安卓打开文件管理器获取文件路径失败,之前可以使用,现在突然就不行了

分类:uni-app

这是打开文件管理器和获取文件路径的的代码
可以获取到视频,音频,图片文件路径,其他文件确获取不到比如pdf,xls,docx获取不到

 export function gainFileChanges (callback, acceptType) {  
    var CODE_REQUEST = 1000;  
    var main = plus.android.runtimeMainActivity();  
    if (plus.os.name == 'Android') {  
        var Intent = plus.android.importClass('android.content.Intent');  
        var intent = new Intent(Intent.ACTION_GET_CONTENT);  
        intent.addCategory(Intent.CATEGORY_OPENABLE);  
        if (acceptType) {  
            intent.setType(acceptType);  
        } else {  
            intent.setType("*/*");  
        }  
        main.onActivityResult = (requestCode, resultCode, data) => {  
            if (requestCode == CODE_REQUEST) {  
                const uri = data.getData();  
                plus.android.importClass(uri);  
                const Build = plus.android.importClass('android.os.Build');  
                const isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;  
                const DocumentsContract = plus.android.importClass('android.provider.DocumentsContract');  
                if (isKitKat && DocumentsContract.isDocumentUri(main, uri)) {  
                    if ("com.android.externalstorage.documents" == uri.getAuthority()) {  
                        console.log("6666");  
                        var docId = DocumentsContract.getDocumentId(uri);  
                        var split = docId.split(":");  
                        var type = split[0];  

                        if ("primary" == type) {  
                            var Environment = plus.android.importClass('android.os.Environment');  
                            callback(Environment.getExternalStorageDirectory() + "/" + split[1]);  
                        } else {  
                            var System = plus.android.importClass('java.lang.System');  
                            var sdPath = System.getenv("SECONDARY_STORAGE");  
                            if (sdPath) {  
                                callback(sdPath + "/" + split[1]);  
                            }  
                        }  
                    } else if ("com.android.providers.downloads.documents" == uri.getAuthority()) {  
                        var id = DocumentsContract.getDocumentId(uri);  
                        var ContentUris = plus.android.importClass('android.content.ContentUris');  
                        var contentUri = ContentUris.withAppendedId(  
                            //    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));  
                            Uri.parse("content://downloads/public_downloads"), id);  
                        callback(getDataColumn(main, contentUri, null, null));  
                    } else if ("com.android.providers.media.documents" == uri.getAuthority()) {  
                        var docId = DocumentsContract.getDocumentId(uri);  
                        var split = docId.split(":");  
                        console.log(split);  
                        var type = split[0];  
                        console.log(type);  
                        var MediaStore = plus.android.importClass('android.provider.MediaStore');  
                        if ("image" == type) {  
                            contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;  
                        } else if ("video" == type) {  
                            contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;  
                        } else if ("audio" == type) {  
                            contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;  
                        } else {  
                            contentUri = MediaStore.Files.getContentUri("external");  
                        }  

                        console.log(contentUri);  
                        var selection = "_id=?";  
                        var selectionArgs = new Array();  
                        selectionArgs[0] = split[1];  

                        callback(getDataColumn(main, contentUri, selection, selectionArgs));  
                    }  
                } else if ("content" == uri.getScheme()) {  
                    callback(getDataColumn(main, uri, null, null));  
                } else if ("file" == uri.getScheme()) {  
                    callback(uri.getPath());  
                }  
            }  
        }  
        main.startActivityForResult(intent, CODE_REQUEST);  
    }  

    function getDataColumn(main, uri, selection, selectionArgs) {  
        plus.android.importClass(main.getContentResolver());  
        let cursor = main.getContentResolver().query(uri, ['_data'], selection, selectionArgs,  
            null);  
        plus.android.importClass(cursor);  
        if (cursor != null && cursor.moveToFirst()) {  
            var column_index = cursor.getColumnIndexOrThrow('_data');  
            var result = cursor.getString(column_index)  
            cursor.close();  
            return result;  
        }  
        return null;  
    }  
}


有什么处理办法吗?

2023-12-28 14:53 负责人:无 分享
已邀请:
DCloud_heavensoft

DCloud_heavensoft

之前和现在分别是什么版本?

  • 我是纸飞机 (作者)

    找到问题了,不是Hbuilder的版本问题,是targetSdkVerion的问题,升级到30之后就有这个问题了,主要在于权限的获取,他只获取了相册中的读取权限,并没有其他地方的读取权限

    2023-12-29 16:20

  • 我是纸飞机 (作者)

    https://ask.dcloud.net.cn/question/184373?column=log&rf=false 这是我新发的问题

    2023-12-29 16:55

要回复问题请先登录注册