How can share file to whatsapp? - java

I used the codes below. When I press the button, whatsapp opens, when I select the person I will share and press on it, the audio file is not sent and the text "sharing failed please try again" appears? Is there a problem with my codes, do not I understand the file format?
there is my raw folder
enter image description here
paylas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri catURI;
catURI = Uri.parse("android.resource://your.app.package/" + R.raw.helikopter);
Intent videoshare = new Intent(Intent.ACTION_SEND);
videoshare.setType("*/*");
videoshare.setPackage("com.whatsapp");
videoshare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
videoshare.putExtra(Intent.EXTRA_STREAM,catURI);
startActivity(videoshare);
}
});

I saw you want to send a .mp3 file. So you need the setType("audio/*")
I made an example with a ringtone. That's how you can send music:
paylas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String sharePath = Environment.getExternalStorageDirectory().getPath()
+ "/Soundboard/Ringtones/custom_ringtone.ogg";
Uri catURI = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, catURI);
startActivity(Intent.createChooser(share, "Share Sound File"));
}
});
Make sure you have internet and External Storage granted in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Hope your question is answered now. Give it a shot, but I bet it works :)

Related

How to share MP3 file + text in Android?

I have tried to make a code for sharing an MP3 file and text at once on Button click, but in reality when I click the share button and choose for example WhatsApp I can only share the MP3 file but not the text with the MP3 at once. Here is the code that I am using to share this both files:
btn_share.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
String sharePath = OUTPUT_PATH+"temp/output/out.mp3";
Uri uri = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("*/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, "Get more info about our band");
startActivity(Intent.createChooser(share, "Share your sound"));
}
});
Is it not possible or is WhatsApp just blocking such actions? Do you know a solution to fix it? Thanks in advance for helping me!

Opening camera with a navigation tab

I just started using Android Studio to develop an experimental application. My current problem in trying to get one of my navigation bar tabs set to a camera tab so that when pressed, the camera opens, user takes a picture, then the program does something with that picture. However, when I tried to set the tab as a camera, the program immediately crashes after clicking on the tab. I have this portion of the code currently set as my Main for that specific tab.
case R.id.navigation.dashboard:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
return true;
Should I use a button as an alternative?
EDIT: changed title and some details to the question for more clarification
EDIT2: Got the problem fixed, thanks! The problem for me was actually not checking for app permissions manually via the app info even though I had these permission scripts on the XML file already.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
So I forgot to add a permission popup on my program.
Couple of thing are wrong with this:
Why use tabs for camera if you are just going to use an intent for the camera picture.
The intent should look something like this:
Note: imageUri should be set here to be used later
void takePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
}
Since starting an activity for result you need to declare a variable TAKE_PICTURE:
private static final int TAKE_PICTURE = 123
Maybe you want to use tab for showing the picture, and that can look like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(getContentResolver(), selectedImage);
// show image here in an imageView
} catch (Exception e) {
Log.e("Camera", e.toString());
}
}
}
}
If you don't want to open new Activity you can use CameraX library.
Here you have nice sample how to use it.

How to share text from android app to facebook?

I try to implement share button in my android app to share in facebook
The code:
facebook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_SUBJECT,desc.getText().toString());
startActivity(Intent.createChooser(share, "Share description using"));
}
});
The problem is, in the post dialog of the facebook not display the text which i need to share from my app which it is (desc.getText().toString()) . There empty message !! why the text didn't appear !!!
Thanks in advance
Try with this. Hope it help
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Share Something");
startActivity(Intent.createChooser(intent, "Share with"));

Sharing text with image to instagram using android intent

I know that this question has been asked several times before, I am trying to add caption to image shared to instagram using send intent
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
shareIntent.putExtra(Intent.EXTRA_TEXT,"YOUR TEXT TO SHARE IN INSTAGRAM");
shareIntent.setPackage("com.instagram.android");
return shareIntent;
Has someone ever managed to make it work?
Is it not supported or has the support been revoked?
There was an official statement from Instagram (mid-2015) announcing that pre-populated captions would no longer be accepted in the iOS and Android apps:
Beginning today, the iOS Hooks and Android Intents will stop accepting captions passed by third party apps. This is a non-breaking change: existing mobile apps that utilize pre-filled captions will continue to be able to use this flow to share media through the Instagram apps, but now Instagram will ignore the caption text. To create a caption for a photo or video shared by a third party app, users will have to enter a caption manually, the same way they already do when sharing content using the Instagram native apps.
Looking at the Instagram documentation for Android, indeed we see that there's no mention of providing the conventional Intent.EXTRA_TEXT string extra in the intent as is customary for other apps. Their sample is limited to only providing a Uri:
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
I'm sorry to say that it simply isn't possible, and we're at the discretion of Facebook in making this decision.
Until it`s not solved by Instagram, I copy the text to the clipboard and instruct the user to paste it
#Override
public void onSingleImageSelected(Uri uri, String tag) {
fileProfileImage = uri.getPath();
compressProfileImage();
imgShareTosocial.setVisibility(View.VISIBLE);
Glide.with(getApplicationContext()).load(uri).into(imgShareTosocial);
}
#SuppressLint("CheckResult")
private void compressProfileImage() {
File file = new File(fileProfileImage);
new Compressor(this)
.compressToFileAsFlowable(file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<File>() {
#Override
public void accept(File file) throws Exception {
compressProfileImage = file;
String imagePath = compressProfileImage.getAbsolutePath();
tvSelectMedia.setText(imagePath);
}
}, new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) throws Exception {
throwable.printStackTrace();
}
});
}
private void shareToInstagram() {
path = tvSelectMedia.getText().toString().trim();
Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
if (intent != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.instagram.android");
try {
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), path, "Step Up", "Step Up")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
shareIntent.setType("image/jpeg");
startActivity(shareIntent);
} else {
// bring user to the market to download the app.
// or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + "com.instagram.android"));
startActivity(intent);
}
}
I'm with the same problem. I think is not possible at this time.
In https://instagram.com/developer/mobile-sharing/android-intents/ only talk about Intent.EXTRA_STREAM, so i suppose that it's the only available.
Here is my code:
Intent instagramIntent = new Intent(Intent.ACTION_SEND);
instagramIntent.setType("image/*");
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
instagramIntent.putExtra(Intent.EXTRA_STREAM, uri);
instagramIntent.setPackage("com.instagram.android");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(instagramIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for(ResolveInfo resolveInfo: resolvedInfoList){
if(resolveInfo.activityInfo.packageName.startsWith("com.instagram.android")){
instagramIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name );
resolved = true;
break;
}
}
if(resolved){
startActivity(instagramIntent);
}else{
Toast.makeText(PromocionarMain.this, "Instagram App is not installed", Toast.LENGTH_LONG).show();
}
Instagram have stopped accepting pre-populated capitions to increase the quality of content in the system. See this post.
http://developers.instagram.com/post/125972775561/removing-pre-filled-captions-from-mobile-sharing

How can I call a number from button press in Android?

I'm very much a beginner at this and I'm struggling to get this to work.
When button is pressed, I simply want the dialer to open with the specified number automatically inputted.
So far I've tried the following:
Button btn_call_us = (Button) findViewById(R.id.btn_call_us);
btn_call_us.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:00000000"));
startActivity(callIntent);
}
});
I've also tried:
Button btn_call_us = (Button) findViewById(R.id.btn_call_us);
btn_call_us.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String phoneno="00000000";
Intent i=new Intent(Intent.ACTION_CALL,Uri.parse(phoneno));
startActivity(i);
}
});
I've added the permission ACTION_CALL to the manifest.
Whenever I click the Call button the app force closes.
Any assistance would be greatly appreciated.
Thank you!
String number = "12345678";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +number));
startActivity(intent);
You need to add this Permission to your manifest.
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
i think you Must add <uses-permission android:name="android.permission.CALL_PHONE" /> in Manifest.
If you want the dialer to open with the number use ACTION_DIAL
Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + phoneno));
You do not need any permission
Make sure you have added the
<uses-permission android:name="android.permission.CALL_PHONE" />
tag at a correct level in the AndroidManifest.xml file (outside the
<application ... /> tag but within the <manifest ... /> tag):
Try this.
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneno)));
Also, add the permission android.permission.CALL_PHONE in your manifest file.
Add the following in the manifest file and it should work fine -
<uses-permission android:name="android.permission.CALL_PHONE"/>
In your Android Phone Go to: "Setting"-> "InstalledApp"-> "find your app"-> "App Permission"-> "Here allow the "Telephone Permission"
Hope it will help you

Categories