我正在尝试使用 Whatsapp/或其他照片共享应用程序将我的应用程序中的示例图片分享给其他人。但不知道为什么不分享。我不知道我的代码哪里做错了。请帮我解决问题。我正在使用 Intent 来分享图像。
我尝试过的代码文件如下->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.IntentBasics"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.IntentBasics">
<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.intentbasics.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>
package com.example.intentbasics
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.content.FileProvider
import com.example.intentbasics.ui.theme.IntentBasicsTheme
import java.io.File
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
IntentBasicsTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val imageName = "SolarSystem.jpg"
val imageFile = File(Environment.getExternalStorageDirectory(), imageName)
// Create a Uri from the file using FileProvider
val imageUri: Uri = FileProvider.getUriForFile(
applicationContext,
"com.example.intentbasics.fileprovider",
imageFile
)
Button(onClick = {
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = "image/*" // Use "image/jpeg" instead of "image/jpg"
putExtra(Intent.EXTRA_STREAM, imageUri)
}
try {
startActivity(Intent.createChooser(shareIntent, "Share Image"))
} catch (e: ActivityNotFoundException) {
Log.d("MainActivity", "Error ${e.message}")
}
}) {
Text(text = "Send")
}
}
}
}
}
}
您需要扩展
FileProvider
并将应用程序清单中的 android:name
属性设置为您创建的 FileProvider,如 文档中所述。
package com.example.intentbasics
class MyFileProvider : FileProvider(R.xml.file_paths)
AndroidManifest.xml
<provider
android:name="com.example.intentbasics.MyFileProvider "
使用
ShareCompat.IntentBuilder
而不是自己创建和启动 Intent
。
Button(onClick = {
ShareCompat.IntentBuilder(context)
.setType("image/jpeg")
.addStream(imageUri)
.setChooserTitle("Share image")
.setSubject("Shared image")
.startChooser()
}) {
Text(text = "Send")
}
WRITE_EXTERNAL_STORAGE
已弃用,不适用于最新的 Android 版本,请添加 READ_MEDIA_IMAGES
权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
另外,不要忘记您需要明确授予相关权限。