【发布时间】:2019-06-10 08:31:28
【问题描述】:
我正在尝试使用以下代码使用不同的应用程序附加图像:
val sendIntent = Intent(Intent.ACTION_SEND)
sendIntent.putExtra(Intent.EXTRA_TEXT, "Test example")
sendIntent.type = "image/png"
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(logo.absolutePath))
startActivity(sendIntent)
附上的图片是用这个代码生成的:
// Generate file for application logo
val path = Environment.getExternalStorageDirectory().absolutePath
val logo = File(path, "logo.png")
// If logo doesn't exist
if (!logo.exists())
{
// Create new file
logo.createNewFile()
// Save application logo to new file
val fOut = FileOutputStream(logo)
val image = BitmapFactory.decodeResource(applicationContext.resources, R.mipmap.ic_launcher_round)
image.compress(Bitmap.CompressFormat.PNG, 100, fOut)
fOut.flush()
fOut.close()
}
但是当我尝试以此意图打开 GMAIL 时,只有文本显示应用程序出现错误:Couldn't attach file。
我错过了什么?
编辑
这是另一个解决方案:android.os.FileUriExposedException: file.jpg exposed beyond app through ClipData.Item.getUri()
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
【问题讨论】:
-
这不是你的错,主要是因为 Gmail 将此流标记为附件,请附上有关 Gmail 的整个错误日志。
-
你应该使用 FileProvider 来获取文件的 Uri
标签: android image android-intent