【问题标题】:Attach image using Intent.ACTION_SEND使用 Intent.ACTION_SEND 附加图像
【发布时间】: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


【解决方案1】:

从Android N你必须使用FileProvider来获取Uri

请参阅下面的文件共享示例。

ArrayList<Uri> files = new ArrayList<Uri>();

        File file = new File(<Your File Path>);
        Uri uri;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(DetailActivity.this, BuildConfig.APPLICATION_ID + ".provider", file);
        } else {
            uri = Uri.fromFile(file);
        }
        files.add(uri);

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        intent.putExtra(Intent.EXTRA_SUBJECT, "Product Sharing");
        intent.setType("image/jpeg");
        intent.putExtra(Intent.EXTRA_TEXT, "ANY TEXT MESSAGE");
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
        startActivity(intent);

将下面的代码放在AndroidManifest.xml的Application标签中

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>

并将filepath.xml文件放在xml资源目录中

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

【讨论】:

    【解决方案2】:

    您无法在没有文件提供程序的情况下发送文件。例如,Gmail 不会请求 READ/WRITE_EXTERNAL_STORAGE 权限,因此它无法访问您的文件。您必须使用带有GRANT_READ_URI_PERMISSION 的文件提供程序。您可以在这里阅读更多内容

    1. https://developer.android.com/reference/android/support/v4/content/FileProvider
    2. https://developer.android.com/training/secure-file-sharing/setup-sharing
    3. https://developer.android.com/training/secure-file-sharing/share-file

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多