【问题标题】:`Intent.ACTION_VIEW` fails with a `no file received` error`Intent.ACTION_VIEW` 失败并出现 `no file received` 错误
【发布时间】:2022-07-30 19:59:44
【问题描述】:

我当前的代码:

只关注fileOpener 变量。 SafeFiles 类是一个自我声明的变量,具有文件的扩展名等。不用太担心。

fileOpener = { file: File ->
    val uri: Uri = Uri.fromFile(file).normalizeScheme()
    val mime = MimeTypeMap
        .getSingleton()
        .getMimeTypeFromExtension(safeFile.extension.substring(1))
    Log.d(TAG, "mime = $mime")
    val intent = Intent(Intent.ACTION_VIEW)
    intent.data = uri
    intent.type = mime
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    Log.d(
        TAG, "data received:" +
                "uri = $uri\n" +
                "extension = ${safeFile.extension.substring(1)}\n" +
                "path = ${file.absolutePath}\n" +
                "size = ${file.totalSpace} "
    )
    CoroutineScope(Dispatchers.Main).launch {
        context!!.startActivity(intent)
    }
}

日志 -

2022-07-30 16:41:41.005 19233-19285/com.example.cryptile D/SafeViewerFragment: mime = image/png
2022-07-30 16:41:41.005 19233-19285/com.example.cryptile D/SafeViewerFragment: data received:uri = file:///storage/emulated/0/Cryptile/CRYPTILE_2022_07_30/.CACHE/aa854c7e-9fa4-4313-9750-cf58fde467b8.png
    extension = png
    path = /storage/emulated/0/Cryptile/CRYPTILE_2022_07_30/.CACHE/aa854c7e-9fa4-4313-9750-cf58fde467b8.png
    size = 119640424448

显然,大小不为零意味着文件存在。我还从设备的文件资源管理器中打开了上述文件,一切似乎都运行良好。我得出的唯一结论是这可能与文件提供程序有关。所以,这是清单 -

清单 -

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

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

    <application
        android:name=".app_data.AppApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.CRYPTILE">
        <activity
            android:name=".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="com.example.cryptile.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
    </application>
</manifest>

还有 filepaths.xml -

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="/storage/emulated/0"
        path="." />
</paths>

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    添加它可能会有所帮助 -

    intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
    

    我的最终代码是 -

    fileOpener = { file: File ->
        val uri: Uri = FileProvider.getUriForFile(
            requireContext(),
            "com.example.cryptile.fileProvider",
            file
        )
        val mime = MimeTypeMap
            .getSingleton()
            .getMimeTypeFromExtension(safeFile.extension.substring(1))
        Log.d(TAG, "mime = $mime")
    
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.action = Intent.ACTION_VIEW
        intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
        intent.setDataAndType(uri, mime)
        Log.d(
            TAG, "data received:" +
                    "uri = $uri\n" +
                    "extension = ${safeFile.extension.substring(1)}\n" +
                    "path = ${file.absolutePath}\n" +
                    "size = ${file.totalSpace} "
        )
        CoroutineScope(Dispatchers.Main).launch {
            context!!.startActivity(intent)
        }
    }
    

    在此 lambda 之外的其他任何地方都没有进行任何更改。只需确保比较意图代码,因为它与问题的代码相比略有不同。

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 2017-07-25
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多