简体   繁体   English

OnActivityResult 始终在 Android 11 上返回 RESULT CANCELED(代码:0)

[英]OnActivityResult always returns RESULT CANCELED (Code: 0) on Android 11

Sadly, Android 11 storage restrictions are too hard and confusing.可悲的是,Android 11 存储限制太难和令人困惑。

I'm trying to pick a document using the below code我正在尝试使用以下代码选择文档

    fun onFile(activity: AppCompatActivity?, fragmentContext: Fragment?, isMultiple: Boolean?, pFileMimeTypes: Array<String>?): Intent {
            val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
                action = Intent.ACTION_OPEN_DOCUMENT
                addCategory(Intent.CATEGORY_OPENABLE)
                putExtra(Intent.EXTRA_ALLOW_MULTIPLE, isMultiple ?: false)
                addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            }
    
            val fileMimeTypes = pFileMimeTypes ?: FileUtil.mimeTypes
            intent.type = if (fileMimeTypes.size == 1) fileMimeTypes[0] else "*/*"
            if (!fileMimeTypes.isNullOrEmpty()) {
                intent.putExtra(Intent.EXTRA_MIME_TYPES, fileMimeTypes)
            }
    
    
            val list = activity?.packageManager?.queryIntentActivities(intent, PackageManager.MATCH_ALL)
            if (list?.size!! > 0) {
                if (fragmentContext == null) {
                    activity.startActivityForResult(
                            Intent.createChooser(
                                    intent,
                                    activity.getString(R.string.m_selectFile_txt)
                            ), FILE_CODE
                    )
                } else {
                    fragmentContext.startActivityForResult(
                            Intent.createChooser(
                                    intent,
                                    activity.getString(R.string.m_selectFile_txt)
                            ), FILE_CODE
                    )
                }
            }
// returning intent is not being used anywhere else
            return intent
        }

Although I have all files access permission Environment.isExternalStorageManager() but still after selecting document(PDF) from the external storage (download or any folder), OnActvityResult is being called with result code 0 .尽管我拥有所有文件访问权限Environment.isExternalStorageManager()但仍然在从外部存储(下载或任何文件夹)中选择文档(PDF)后, OnActvityResult被调用, result code 0 I need the Uri of the selected document to proceed further.我需要所选文档的 Uri 才能继续进行。

For any other reference, below is my manifest file对于任何其他参考,以下是我的清单文件

<?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="*****Hidden*******">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />
    <uses-permission android:name="android.permission.READ_STORAGE_PERMISSION"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"  />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity
            android:exported="true"
            android:name=".Main2Activity"
            android:launchMode="singleTask"
            android:label="@string/title_activity_main2"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar">

        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.attachmentmanager"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="android:authorities">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider"
                tools:replace="android:resource" />
        </provider>
    </application>



    <queries>
        <intent>
            <action android:name="android.intent.action.OPEN_DOCUMENT" />
            <!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
            <data android:mimeType="*/*" />
        </intent>
        <intent>
            <action android:name="android.intent.action.PICK" />
            <!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
            <data android:mimeType="*/*" />
        </intent>
    </queries>


</manifest>

I must be missing some point.我一定错过了一些观点。 Everything is working fine below Android 11 Android 11以下一切正常

Thanks in Advance.提前致谢。

startActivityForResult is deprecated startActivityForResult 已弃用

Refer this link参考这个链接

Use Activity Results API Official Docs使用活动结果 API官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM