#java #android #pdf #android-intent #whatsapp
#java #Android #PDF #android-намерение #whatsapp
Вопрос:
Опробовал следующие фрагменты кода для отправки pdf, изображения и текста. При использовании ACTION_VIEW открывается конкретный чат WhatsApp, текст отправляется, но не может отправить PDF или изображение. Пожалуйста, помогите с соответствующим фрагментом кода для следующей проблемы, с которой я столкнулся.
Методы, которые я пробовал, следующие:
PackageManager pm = bill_layout.this.getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("application/pdf");
String text = "YOUR TEXT HERE";
PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(bill_layout.this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
try {
String longUrl="https://firebasestorage.googleapis.com/v0/b/glossy-radio-280106.appspot.com/o/19c48c9d-3f83-4a8f-a296-fe0ea
Bitly bitly= Bit.ly("o_1gduh4caf3");
shorturl=bitly.shorten(longUrl);
String text= "shorturl";// Replace with your message.
String toNumber = "91xxxxxxxxxx"; // Replace with mobile phone number without Sign or leading zeros, but with country code
//Suppose your country is India and your phone number is “xxxxxxxxxx”, then you need to send “91xxxxxxxxxx”.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("http://api.whatsapp.com/send?phone=" toNumber "amp;text=" URLEncoder.encode(text, "UTF-8"))
"text/plain");
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT,"okay");
intent.setPackage("com.whatsapp");
startActivity(intent); }
catch (Exception e){
e.printStackTrace();}
PackageManager pm1=getPackageManager();
try {
String toNumber = "91xxxxxxxxxx"; // Replace with mobile phone number without Sign or leading zeros, but with country co
//Suppose your country is India and your phone number is “xxxxxxxxxx”, then you need to send “91xxxxxxxxxx”.
Intent sendIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:" toNumber));
sendIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_TEXT,"test1");
sendIntent.putExtra("chat",true);
startActivity(sendIntent);}
catch (Exception e){
e.printStackTrace();
Toast.makeText(bill_layout.this,"it may be you dont have whats app",Toast.LENGTH_LONG).show();
}
Intent sendIntent = new Intent("android.intent.action.SEND");
File f=new File("path to the file");
Uri uri = Uri.parse("gs://glossy-radio-280106.appspot.com/19c48c9d-3f83-4a8f-a296-fe0eaf4488c4.pdf");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker"));
sendIntent.setType("application/pdf");
sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
sendIntent.putExtra("jid","918824525121" "@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT,"sample text you want to send along with the image");
startActivity(sendIntent);
PackageManager pm = bill_layout.this.getPackageManager();
String formattedNumber ="91xxxxxxxxxx";
try{
Intent sendIntent =new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Card Set ");
sendIntent.putExtra(Intent.EXTRA_TEXT,formattedNumber);
sendIntent.setType("text/plain");
sendIntent.putExtra("jid", formattedNumber "@s.whatsapp.net");
sendIntent.setPackage("com.whatsapp");
bill_layout.this.startActivity(sendIntent);
}
catch(Exception e)
{
Toast.makeText(bill_layout.this,"Error/n" e.toString(),Toast.LENGTH_SHORT).show();
Ответ №1:
Попробуйте это, задайте свой тип, то есть файл, изображение или текст.
Intent sendIntent = new Intent("android.intent.action.SEND");
File f=new File("Your file path");
Uri uri = Uri.fromFile(f);
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker"));
sendIntent.setType("image");
sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("91xxxxxxxxxx") "@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT,"Sample text");
startActivity(sendIntent);