如何共享的WhatsApp Android的图像? [英] How to share an image with WhatsApp Android?

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

问题描述

我想这code将图像发送到WhatsApp的,但是当它打开WhatsApp的屏幕,当我选择联系人,我没有看到任何图像。有人能帮助我吗?

 位图进阶= BitmapFactory.de codeResource(getResources(),R.drawable.image3);
    意图份额=新意图(Intent.ACTION_SEND);
    share.setType(图像/ JPG);
    ByteArrayOutputStream字节=新ByteArrayOutputStream();
    adv.com preSS(Bitmap.Com pressFormat.JPEG,100字节);
    文件f =新的文件(Environment.getExternalStorageDirectory()
            +文件分割符+temporary_file.jpg);
    尝试{
        f.createNewFile();
        新的FileOutputStream(F).WRITE(bytes.toByteArray());    }赶上(IOException异常五){
        e.printStackTrace();
    }
    乌里imageUri = Uri.parse(f.getAbsolutePath());
    share.putExtra(Intent.EXTRA_STREAM,Uri.parse(Environment.getExternalStorageDirectory()+文件分割符+temporary_file.jpg));
    //share.putExtra(Intent.EXTRA_STREAM,Uri.parse(\"file:///android_asset/image3.jpg));
    如果(isPackageInstalled(com.whatsapp,这)){
        share.setPackage(com.whatsapp);
        startActivity(Intent.createChooser(份额,分享图片));    }其他{        Toast.makeText(getApplicationContext(),请安装了WhatsApp,Toast.LENGTH_LONG).show();
    }}私人布尔isPackageInstalled(字符串软件包名,上下文的背景下){
    软件包管理系统下午= context.getPackageManager();
    尝试{
        pm.getPackageInfo(软件包名,PackageManager.GET_ACTIVITIES);
        返回true;
    }赶上(PackageManager.NameNotFoundException E){
        返回false;
    }
}


解决方案

试试这个code.In这个我自该绘制folder.Instead发送ic_launcher您可以使用您的位图或文件。

\r
\r

无效份额(){\r
位图图标= BitmapFactory.de codeResource(getResources()\r
R.drawable.ic_launcher);\r
意图份额=新意图(Intent.ACTION_SEND);\r
share.setType(图像/ JPEG);\r
ByteArrayOutputStream字节=新ByteArrayOutputStream();\r
icon.com preSS(Bitmap.Com pressFormat.JPEG,100字节);\r
文件f =新的文件(Environment.getExternalStorageDirectory()\r
+文件分割符+temporary_file.jpg);\r
尝试{\r
f.createNewFile();\r
FileOutputStream中FO =新的FileOutputStream(F);\r
fo.write(bytes.toByteArray());\r
}赶上(IOException异常五){\r
e.printStackTrace();\r
}\r
share.putExtra(Intent.EXTRA_TEXT,你好#TEST);\r
\r
share.putExtra(Intent.EXTRA_STREAM,\r
Uri.parse(文件:///sdcard/temporary_file.jpg));\r
share.setPackage(com.whatsapp);\r
startActivity(Intent.createChooser(份额,分享图片));\r
}

\r

\r
\r

I'm trying this code to send images to WhatsApp, but when it opens the WhatsApp screen and when I select the contact, I don't see any image. Can someone help me?

    Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.image3);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        new FileOutputStream(f).write(bytes.toByteArray());

    } catch (IOException e) {
        e.printStackTrace();
    }
    Uri imageUri = Uri.parse(f.getAbsolutePath());
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"));
    //share.putExtra(Intent.EXTRA_STREAM,Uri.parse("file:///android_asset/image3.jpg"));
    if(isPackageInstalled("com.whatsapp",this)){
        share.setPackage("com.whatsapp");
        startActivity(Intent.createChooser(share, "Share Image"));

    }else{

        Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show();
    }

}

private boolean isPackageInstalled(String packagename, Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

解决方案

try this code.In this I am sending ic_launcher from drawable folder.Instead of that you can use your bitmap or file.

void share() {
		Bitmap icon = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher);
		Intent share = new Intent(Intent.ACTION_SEND);
		share.setType("image/jpeg");
		ByteArrayOutputStream bytes = new ByteArrayOutputStream();
		icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
		File f = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "temporary_file.jpg");
		try {
			f.createNewFile();
			FileOutputStream fo = new FileOutputStream(f);
			fo.write(bytes.toByteArray());
		} catch (IOException e) {
			e.printStackTrace();
		}
		share.putExtra(Intent.EXTRA_TEXT, "hello #test");
		
		share.putExtra(Intent.EXTRA_STREAM,
				Uri.parse("file:///sdcard/temporary_file.jpg"));
		share.setPackage("com.whatsapp");
		startActivity(Intent.createChooser(share, "Share Image"));
	}

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

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