如何使用“共享图像”分享 意图在android中分享图像?
我有一个图像厨房应用程序;在该应用程序中,我将所有图像放入drawable-hdpi 文件夹中。我在活动中调用图像是这样的:
private Integer[] imageIDs = {
R.drawable.wall1, R.drawable.wall2,
R.drawable.wall3, R.drawable.wall4,
R.drawable.wall5, R.drawable.wall6,
R.drawable.wall7, R.drawable.wall8,
R.drawable.wall9, R.drawable.wall10
};
如何使用共享意图来共享这些图像?我这样放置共享代码:
Button shareButton = (Button) findViewById(R.id.share_button);
shareButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(Images.Media.EXTERNAL_CONTENT_URI + "/" + imageIDs);
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
});
而且我也有一个共享按钮;当我单击共享按钮时,共享框打开,但是当我单击任何服务时,大多数情况下它会崩溃或某些服务显示:“无法打开图像”。我该如何解决这个问题,或者是否有其他格式代码来共享图像?
我尝试使用下面的代码,但它不起作用。
Button shareButton = (Button) findViewById(R.id.share_button);
shareButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("android.resource://com.android.test/*");
try {
InputStream stream = getContentResolver().openInputStream(screenshotUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
});
如何共享 drawable-hdpi 文件夹中的图像?
I have an image galley app; in that app I placed all the images into the drawable-hdpi folder. I call images in my activity like this:
private Integer[] imageIDs = {
R.drawable.wall1, R.drawable.wall2,
R.drawable.wall3, R.drawable.wall4,
R.drawable.wall5, R.drawable.wall6,
R.drawable.wall7, R.drawable.wall8,
R.drawable.wall9, R.drawable.wall10
};
How do I share these images using sharing Intent? I put sharing code like this:
Button shareButton = (Button) findViewById(R.id.share_button);
shareButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(Images.Media.EXTERNAL_CONTENT_URI + "/" + imageIDs);
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
});
And I have a sharing button also; when I click on the share button, the Sharing box is opening, but when I click any service, mostly it's crashing or some services say: "unable to open image". How can I fix this or is there any other format code to share images?
I tried using the code below, but it's not working.
Button shareButton = (Button) findViewById(R.id.share_button);
shareButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("android.resource://com.android.test/*");
try {
InputStream stream = getContentResolver().openInputStream(screenshotUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
});
How do I share my images from the drawable-hdpi folder?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
superM 提出的解决方案对我来说工作了很长一段时间,但最近我在 4.2 (HTC One) 上测试了它,它在那里停止工作。我知道这是一种解决方法,但它是唯一适用于我所有设备和版本的解决方法。
根据文档,开发人员被要求 “使用系统 MediaStore” 发送二进制内容。然而,这有一个(不利)优点,即媒体内容将永久保存在设备上。
如果您可以选择此选项,您可能需要授予
WRITE_EXTERNAL_STORAGE
权限并使用系统范围的 MediaStore。The solution proposed from superM worked for me for a long time, but lately I tested it on 4.2 (HTC One) and it stopped working there. I am aware that this is a workaround, but it was the only one which worked for me with all devices and versions.
According to the documentation, developers are asked to "use the system MediaStore" to send binary content. This, however, has the (dis-)advantage, that the media content will be saved permanently on the device.
If this is an option for you, you might want to grant permission
WRITE_EXTERNAL_STORAGE
and use the system-wide MediaStore.我发现最简单的方法是使用 MediaStore 临时存储您要共享的图像:
来自: 使用意图共享内容
I found the easiest way to do this is by using the MediaStore to temporarily store the image that you want to share:
From: Sharing Content With Intents
首先
添加权限
资源中的位图
使用通过蓝牙和其他信使测试的
First add permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
using Bitmap from resources
Tested via bluetooth and other messengers
谢谢大家,我尝试了给出的几个选项,但这些选项似乎不适用于最新的 android 版本,因此添加适用于最新 android 版本的修改步骤。这些基于上面的一些答案,但进行了修改和修改。该解决方案基于文件提供程序的使用:
步骤:1
在清单文件中添加以下代码:
步骤:2
在 res 中创建 XML 文件 > xml
在 xml 中创建 file_provider_paths 文件。
请注意,这是我们在上一步中包含在 android:resource 中的文件。
在 file_provider_paths 中写入以下代码:
步骤:3
之后,转到您的按钮 单击:
有关更多详细说明,请访问 https://droidlytics.wordpress.com/2020/08/04/use-fileprovider-to-share-image-from-recyclerview/
Thanks all, I tried the few of the options given, but those seems not to work for the latest android releases, so adding the modified steps which work for the latest android releases. these are based on few of the answers above but with modifications & the solution is based on the use of File Provider :
Step:1
Add Following code in Manifest File:
step:2
Create an XML File in res > xml
Create file_provider_paths file inside xml.
Note that this is the file which we include in the android:resource in the previous step.
Write following codes inside the file_provider_paths:
Step:3
After that go to your button Click:
For More detailed explanation Visit https://droidlytics.wordpress.com/2020/08/04/use-fileprovider-to-share-image-from-recyclerview/
简单且最简单的代码,您可以使用它来共享图库中的图像。
Simple and Easiest code you can use it to share image from gallery.
如何在android中以编程方式共享图像,有时您想拍摄视图快照然后分享它
因此,请按照以下步骤操作: 1.向 mainfest 文件添加权限
2.非常首先截取您的视图的屏幕截图,例如它是
Imageview、Textview、Framelayout、LinearLayout 等
例如,您有一个图像视图来截屏
在 oncreate() 中调用此方法
截图后 在按钮上调用共享图像方法
单击 或 您想要的位置
在创建方法后定义这两个方法##
How to share image in android progamatically , Sometimes you wants to take a snapshot of your view and then like to share it
so follow these steps: 1.Add permission to mainfest file
2.Very First take a screenshot of your view , for example it is
Imageview, Textview, Framelayout,LinearLayout etc
For example you have an image view to take screenshot
call this method in oncreate()
after taking screenshot call share image method either on button
click or where you wants
After on create method define these two method ##
这是一个对我有用的解决方案。一个问题是您需要将图像存储在共享或非应用程序私有位置(http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)
许多建议都说存储在
Apps
“私有”缓存中位置,但这当然不是可通过其他外部应用程序访问,包括正在使用的通用共享文件意图。当您尝试此操作时,它将运行,但例如 dropbox 会告诉您该文件不再可用。/* 步骤 1 - 使用下面的文件保存函数在本地保存位图文件。 */
/* 步骤 2 - 将非私有绝对文件路径共享到共享文件意图 */
/* 保存图像功能 */
/* 步骤 3 - 处理共享文件意图结果。需要远程临时文件等。 */
我希望对某人有所帮助。
Here is a solution that worked for me. One gotcha is you need to store the images in a shared or non app private location (http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)
Many suggestions say to store in the
Apps
"private" cache location but this of course is not accessable via other external applications, including the generic Share File intent which is being utilised. When you try this, it will run but for example dropbox will tell you the file is no longer available./* STEP 1 - Save bitmap file locally using file save function below. */
/* STEP 2 - Share the non private Absolute file path to the share file intent */
/* SAVE IMAGE FUNCTION */
/* STEP 3 - Handle Share File Intent result. Need to remote temporary file etc. */
I hope that helps someone.
我厌倦了搜索从我的应用程序到其他应用程序共享视图或图像的不同选项。最后我得到了解决方案。
步骤一:
共享意图处理块。这将弹出您的窗口,其中包含手机中的应用程序列表
第 2 步:
将视图转换为位图
第 3 步:
从位图图像获取 URI
I was tired in Searching different options for sharing view or image from my application to other application . And finally i got the solution.
Step 1 :
Share Intent handling Block. This will Pop Up your window with list of Applications in you phone
Step 2 :
Converting your view to BItmap
Step 3 :
To get The URI from Bitmap Image
我刚刚遇到了同样的问题。
这是一个在主代码中不使用任何显式文件写入的答案(让 api 为您处理)。
这就是路径...您只需将图像 ID 添加到 Drawable 对象中即可。
在我的例子中(上面的代码),可绘制对象是从 ImageView 中提取的。
I just had the same problem.
Here is an answer that doesn't use any explicit file writing in your main code (letting the api taking care of it for you).
This is the path... you just need to add your image IDs in a Drawable object.
In my case (code above), the drawable was extracted from an ImageView.
SuperM 答案对我有用,但使用 Uri.fromFile() 而不是 Uri.parse()。
对于 Uri.parse(),它仅适用于 Whatsapp。
这是我的代码:
Uri.parse() 的输出:
/storage/emulated/0/Android/data/application_package/Files/17072015_0927.jpg
Uri.fromFile 的输出:
文件:///storage/emulated/0/Android/data/application_package/Files/17072015_0927.jpg
SuperM answer worked for me but with Uri.fromFile() instead of Uri.parse().
With Uri.parse(), it worked only with Whatsapp.
This is my code:
Output of Uri.parse():
/storage/emulated/0/Android/data/application_package/Files/17072015_0927.jpg
Output of Uri.fromFile:
file:///storage/emulated/0/Android/data/application_package/Files/17072015_0927.jpg
参考:- http://developer.android.com/training/共享/send.html#send-multiple-content
ref :- http://developer.android.com/training/sharing/send.html#send-multiple-content
上述所有解决方案在 Android Api 26 和 Android Api 26 中都不适用于我。 27 (Oreo),出现
错误:通过 ClipData.Item.getUri 暴露在应用程序之外
。适合我情况的解决方案是FileProvider.getUriForFile(Context,packagename,File)
作为在Manifest 中定义
as
。 xmlresource
文件All the Above solution doesnot work for me in
Android Api 26 & 27 (Oreo)
, was getttingError: exposed beyond app through ClipData.Item.getUri
. The solution that fits in my situation wasFileProvider.getUriForFile(Context,packagename,File)
as<provider>
in yourManifest.xml
asresource
file for your directorys通过 Intent 共享文本和图像的完美解决方案是:
在共享按钮上单击:
然后创建 shareBitmap(image) 方法。
然后测试一下..!!
A perfect solution for share text and Image via Intent is :
On your share button click :
Then Create shareBitmap(image) method.
And then just test It..!!
通过实施更严格的安全策略,在应用程序外部公开 uri 会引发错误并导致应用程序崩溃。
@Ali Tamoor 的回答解释了使用文件提供程序,这是推荐的方式。
有关更多详细信息,请参阅 - https://developer.android.com/training /secure-file-sharing/setup-sharing
此外,您还需要将 androidx 核心库包含到您的项目中。
当然,这是一个有点庞大的库,只需要它来共享文件——如果有更好的方法,请告诉我。
With implementation of stricter security policies, exposing uri outside of app throws an error and application crashes.
@Ali Tamoor's answer explains using File Providers, this is the recommended way.
For more details see - https://developer.android.com/training/secure-file-sharing/setup-sharing
Also you need to include androidx core library to your project.
Of course this is bit bulky library and need it just for sharing files -- if there is a better way please let me know.