简体   繁体   English

是否可以通过数据网址在Android上分享图片?

[英]Is it possible to share an image on Android via a data URL?

Is it possible to share an image with code something like this? 是否可以与这样的代码共享图像?

Intent share = new Intent(Intent.ACTION_SEND);
share.setData(Uri.parse("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACx"
    + "jwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAGoSURBVDhPlVM9SEJRGD2DkL+QmoiKYNggToJL"
    + "4KKLNDiKW4NCoENQkzS1uboILoKjW61NSZOjo1uOBlHPgjJsuN3jvc+naVQHPt79vu+cc9/37n34"
    + "BQkd/0Jibw+3Ozt4j0TwwuCaNfYU5Qe43Tg5OMDD9TWEYUAIoYJr1mIxPEmjc01fh8uF03wer6vC"
    + "7zGbQZRKmPp8uNCyJWKhEAwSTDJ3NNc07fUsE76J1FjjcL5VQb0O4XBAVCoqj0ZV3u2qvN+H8Psx"
    + "1HLAZsPn6u6pFIQsi3AYYjKBcDpVXiyqPrl2O2ayZqc+FY9jaooZuZwSJBJL8iKv1SwONdTSIJtO"
    + "w1g1GA4h9vfVk3mjod5mPLY4ySQMqT2kgZ0jsMjZBgOL9D1omM2qtfwmH1K7SwN4vbhnkzuQ0G5v"
    + "imnOHjkMjwePC7HGUSajxuDM1aqa/+wM4vJSCRnmHSkU8CwNjrVWIRDAVauFubnjaATRbCoD7m7W"
    + "Ox3Mg0HcaNkaduV9GPCmbbuNrLEnxXeSuzi+reD/wJ+Hx1Qu443BNWvsadqfwDOu6lic9yaAL2uK"
    + "Y4RMd4E2AAAAAElFTkSuQmCC"));
startActivity(Intent.createChooser(share, getString(R.id.menu_share)));

My goal is to make my app not require permissions to write to storage, but to be able to share images that it dynamically creates. 我的目标是让我的应用程序不需要写入存储的权限,但能够共享它动态创建的图像。 Is there any way to do this? 有没有办法做到这一点?

Is it possible to share an image with code something like this? 是否可以与这样的代码共享图像?

No, because ACTION_SEND does not use the Uri . 不,因为ACTION_SEND不使用Uri It uses EXTRA_TEXT or EXTRA_STREAM . 它使用EXTRA_TEXTEXTRA_STREAM

My goal is to make my app not require permissions to write to storage, but to be able to share images that it dynamically creates. 我的目标是让我的应用程序不需要写入存储的权限,但能够共享它动态创建的图像。 Is there any way to do this? 有没有办法做到这一点?

Create a ContentProvider to serve the file, then put the Uri pointing to your file within your provider in EXTRA_STREAM . 创建一个ContentProvider来提供文件,然后将Uri指向您的提供者在EXTRA_STREAM文件。 You may be able to protect the provider with a custom permission and allow the sending activity temporary access to it via FLAG_GRANT_READ_URI_PERMISSION , though I have only tried that with Intent structures that use the actual Uri (eg, setData() ) instead of via an extra like EXTRA_STREAM . 可以使用自定义权限保护提供程序,并允许发送活动通过FLAG_GRANT_READ_URI_PERMISSION临时访问它,尽管我只尝试使用实际Uri Intent结构(例如, setData() )而不是通过额外的喜欢EXTRA_STREAM

This sample project demonstrates this technique using ACTION_VIEW (note: requires a device with a PDF viewer installed to truly work). 此示例项目使用ACTION_VIEW演示此技术(注意:需要安装了PDF查看器的设备才能真正起作用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM