如何在Android中将图像URL共享到WhatsApp? [英] How to share image URL to WhatsApp in Android?

查看:449
本文介绍了如何在Android中将图像URL共享到WhatsApp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的应用程序中将图像共享给WhatsApp.我有图片网址.为了与WhatsApp共享,我使用以下代码.

I need to share an image to WhatsApp in my app. I have the image URL. For sharing to WhatsApp, I am using the following code.

String image_url = "http://images.cartradeexchange.com//img//800//vehicle//Honda_Brio_562672_5995_6_1438153637072.jpg";

URI uri = null;
try {
    uri = new URI(image_url.toString());
} catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Log.e("uri=", "" + uri);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/jpg");

// Target whatsapp:
shareIntent.setPackage("com.whatsapp");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

try {
    startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(InventoryManageOnlineActivity.this,
            "Whatsapp have not been installed.",
            Toast.LENGTH_SHORT).show();
}

但是没有用.我收到以下错误.

But it didn't work. I am getting the following error.

08-26 11:57:34.674: W/Bundle(19447): Key android.intent.extra.STREAM expected Parcelable but value was a java.net.URI.  The default value <null> was returned.
08-26 11:57:34.738: W/Bundle(19447): Attempt to cast generated internal exception:
08-26 11:57:34.738: W/Bundle(19447): java.lang.ClassCastException: java.net.URI cannot be cast to android.os.Parcelable
08-26 11:57:34.738: W/Bundle(19447):    at android.os.Bundle.getParcelable(Bundle.java:810)
08-26 11:57:34.738: W/Bundle(19447):    at android.content.Intent.getParcelableExtra(Intent.java:4850)
08-26 11:57:34.738: W/Bundle(19447):    at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7548)
08-26 11:57:34.738: W/Bundle(19447):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1545)
08-26 11:57:34.738: W/Bundle(19447):    at android.app.Activity.startActivityForResult(Activity.java:3746)
08-26 11:57:34.738: W/Bundle(19447):    at android.app.Activity.startActivityForResult(Activity.java:3707)
08-26 11:57:34.738: W/Bundle(19447):    at android.app.Activity.startActivity(Activity.java:4027)
08-26 11:57:34.738: W/Bundle(19447):    at android.app.Activity.startActivity(Activity.java:3989)
08-26 11:57:34.738: W/Bundle(19447):    at com.cartradrexchange.inventory.InventoryManageOnlineActivity$ManageOnlineAdapter.onClick(InventoryManageOnlineActivity.java:441)
08-26 11:57:34.738: W/Bundle(19447):    at android.view.View.performClick(View.java:4761)
08-26 11:57:34.738: W/Bundle(19447):    at android.view.View$PerformClick.run(View.java:19767)
08-26 11:57:34.738: W/Bundle(19447):    at android.os.Handler.handleCallback(Handler.java:739)
08-26 11:57:34.738: W/Bundle(19447):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-26 11:57:34.738: W/Bundle(19447):    at android.os.Looper.loop(Looper.java:135)
08-26 11:57:34.738: W/Bundle(19447):    at android.app.ActivityThread.main(ActivityThread.java:5312)
08-26 11:57:34.738: W/Bundle(19447):    at java.lang.reflect.Method.invoke(Native Method)
08-26 11:57:34.738: W/Bundle(19447):    at java.lang.reflect.Method.invoke(Method.java:372)
08-26 11:57:34.738: W/Bundle(19447):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
08-26 11:57:34.738: W/Bundle(19447):    at com.android.internal.os.ZygoteInit.main(Zygote

为此,我在Google中搜索了很多内容,并遵循了以下网址

For that, I searched a lot in Google, and I followed the below URL

Android共享图像不起作用.

但这对我没有用.请告诉我如何共享图片网址.

But it is not useful for me. Please show me how to share image URLs.

推荐答案

签出此答案

顺便说一句,我不明白你为什么要image_url.toString()?它已经在字符串中了吗?

BTW i dont get it why are you doing image_url.toString() ? It is already in string right ?

也尝试此代码.

String image_url = "http://images.cartradeexchange.com//img//800//vehicle//Honda_Brio_562672_5995_6_1438153637072.jpg";

        Intent shareIntent = new Intent();
        shareIntent.setType("image/*");
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.setAction(Intent.ACTION_SEND);
        //without the below line intent will show error
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, image_url);
                // Target whatsapp:
        shareIntent.setPackage("com.whatsapp");
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                try {
                    startActivity(shareIntent);
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(InventoryManageOnlineActivity.this,
                            "Whatsapp have not been installed.",
                            Toast.LENGTH_SHORT).show();
                }

这篇关于如何在Android中将图像URL共享到WhatsApp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆