android - 转发图像 URI Intent

标签 android android-intent

我正在开发一个填充电子邮件模板并添加图片的应用程序。我想按如下方式使用该应用程序:从图库中,我通过 ACTION_SEND 与我的应用程序共享图像。我的应用会进行一些处理,然后共享一封带附件的电子邮件。

现在我试图简单地获取图像 Uri 并将其传递,但​​随后我得到了一个 SecurityException。

protected void onCreate(Bundle savedInstanceState) {
   // Read the incoming intent
   Intent intent = getIntent();
   String action = intent.getAction();
   String type = intent.getType();
   Uri imageUri;

   if (Intent.ACTION_SEND.equals(action) && type != null) {
        if (type.startsWith("image/")) {
            imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
        }
    }

   // Create the outgoing intent
   Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setType("message/rfc822");
    if(imageUri != null){
        emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    }

    startActivity(Intent.createChooser(emailIntent, getString(R.string.chooserText)));
}

记录:

02-12 14:07:11.196 10241-10241/? E/ResolverActivity: Unable to launch as uid 10156 package ***, while running in android:ui
java.lang.SecurityException: Uid 10156 does not have permission to uri 0 @ content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F19546/FORMAT_JPEG/778368258

这样做的正确方法是什么?我试过设置标志和权限,但找不到具体的操作方法。

最佳答案

from the image library, I share a picture with my app

你没有解释这是什么意思。我将此解释为您在应用中添加了 ACTION_SEND Activity ,并且您正在使用图片库应用中的一些“共享”选项将图片发送到您的应用。

What would be the right way to do this?

在某种程度上,没有正确的方法来做到这一点。您的选择是:

  1. 将图像复制到您的应用中(例如,复制到内部存储中),然后共享本地副本(例如,通过 FileProvider)

  2. 不要使用ACTION_SEND 来接收图像。相反,使用 MediaStore 查找图像并使用这些图像呈现您自己的 UI。然后,通过 ACTION_SEND 发送所选图像的 Uri。这可能行得通,但我对其他两个选项的信心不足。

  3. 不要使用 ACTION_SEND 来发送电子邮件,而是使用 JavaMail 或一些等效的库(要求用户给你很多讨厌的东西,比如电子邮件密码),或者使用你的 Web服务器发送它(要求您将图像上传到您的服务器)。

  4. 不要编写此应用,因为用户可以通过电子邮件自己发送图像,从他们用于触发您的应用的“图像库”应用中的相同“共享”菜单。

问题是你在这里有三个应用程序:A、B 和 C:

  • A是图片库

  • B 是您的应用

  • C 是您尝试通过 ACTION_SEND

  • 调用的电子邮件客户端

当 A 使用 ACTION_SEND 调用 B 时,A 授予 B 使用图像的权利。但是,B 没有能力回过头来授予 C 使用 A 的图像的权利。

关于android - 转发图像 URI Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35363462/

相关文章:

android - 后退按钮上的刷新 Activity

java - Android baseAdapter 在向上滚动时停止评估 if 语句

java - 如何获取Seekbar的值进行计算?

android - Dagger2 - "is not a framework type."错误

c# - SetColorFilter 在 Xamarin.Android 中不起作用

android - CheckBox setSelected 在使用 android :drawableRight property 时不起作用

java - 由于构造函数错误无法启动 ListView

android - SecurityException : Permission Denial: not allowed to send broadcast android. intent.action.BATTERY_CHANGED

android - 如何为使用 Action_View Intent 调用的默认视频播放器禁用 onbackpressed()

android - 如何在我的 android 应用程序中以编程方式打开 GPS?