我正在共享两个URI图像资源,它来自mipmap和ACTION_GET_CONTENT使用的URI。
public void shareUsingIntent() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getUri());
startActivity(Intent.createChooser(i, "Share Image"));
}
public Uri getUri() {
if (selectedImageUri != null) {
return selectedImageUri;
} else {
return Uri.parse("android.resource://" + getPackageName() + "/" + R.mipmap.ic_launcher);
}
}
它在ACTION_GET_CONTENT中使用了URI,但mipmap资源在某些应用程序(如Facebook和watsapp)中无效。我从一些堆栈回答中读到Image必须在Extenrnal存储中添加。它不适合这个URI。
Uri.parse("android.resource://" + getPackageName() + "/" + R.mipmap.ic_launcher
在什么应用程序和Facebook以及它为什么在其他应用程序中工作,如默认的混乱应用程序,Twitter等。
答案 0 :(得分:1)
试试这个功能:
//如果targetSDK&gt; = 23,请检查运行时权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
同时为您的清单文件添加相同的权限。
private void shareViaWhatsApp() {
Uri imageUri = null;
try {
imageUri = Uri.parse(MediaStore.Images.Media.insertImage(this.getContentResolver(),
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher), null, null)); //You may need to check for permission for this.
} catch (NullPointerException e) {
}
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
emailIntent.putExtra(Intent.EXTRA_TEXT, "Text to share");
emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri); //............Pass Image URI here.........
emailIntent.setType("image/*");
emailIntent.setPackage("com.whatsapp");
startActivity(Intent.createChooser(emailIntent, "Share..."));
}
答案 1 :(得分:0)
你可以试试这个:
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"http://play.google.com/store/apps/details?id=" + getPackageName());
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), getBitMap(), "I am Happy", "Share happy !")));
sendIntent.setType("image/png");
sendIntent.setPackage("com.whatsapp");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(sendIntent);
} else {
Toast.makeText(getApplicationContext(), "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
startActivity(goToMarket);
}
其中getBitMap()是获取要共享的图像位图的函数。
答案 2 :(得分:0)
对于您可以共享图片的每个应用使用以下代码,例如什么应用,远足或邮件等等
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.mipmap.ic_launcher);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, This is test Sharing");
startActivity(Intent.createChooser(shareIntent, "Send your image"));