在其他应用程序中共享图像 [英] Sharing images in other application
问题描述
我有带有图像的应用程序,并且希望共享由用户在其他一些应用程序中选择的图像.从这里的其他问题中,我知道我必须将图像放置在公共场所,以便其他应用程序可以访问它.但是我仍然收到错误消息:没有应用程序可以执行此操作",我在哪里出错了?将图片复制到SD卡的代码:
I have application with images and I want share image which is choosen by user in some other application. From other question here I know that I must put the image in public place so it can be accesed by other application. But I still get error "no application can perform this action" any idea where am I doing mistake? Code for copying image to SD card:
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path,String.valueOf(idOfImage));
if (!file.exists())
{
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),idOfImage);
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e)
{
e.printStackTrace();
} finally {
try {
if (out != null)
{
out.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
用于发送意图和选择选择器的代码:
Code for sending the intent and picking chooser:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Uri uri = Uri.fromFile(file);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
StartActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.share)));
感谢答案.
sharedIntent.setType("image/png")时工作正常使用Gmail和G +添加了泳道,但不适用于Messengers FB和其他人.
Works fine when sharingIntent.setType("image/png"); lane added, with Gmail and G+ , but doesnt work with Messengers FB and others.
推荐答案
尝试此代码
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)
v.getTag(R.string.app_name));
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); // put your image URI
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("facebook"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
编辑1
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(filePath))); //optional//use this when you want to send an image
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
使用此代码我尝试过,它可以在我这边工作
use this code i tried it and it works here at my side
这篇关于在其他应用程序中共享图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!