How can I send audio files from my app to Whatsapp? - java

I'm working on an app from which you can send audio to whatsapp. However, the codes that I am using are not working.
//This code returns an error and asks to try again to send the audio file
String sharePath = Environment.getExternalStorageDirectory().getPath()
+ "android.resource://"
+ wspActivity.this.getPackageName() + "/raw/" + "increible";
Uri uri = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage("com.whatsapp");
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));
//This code indicates that the file is of an incompatible type.
final Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/mp3");
final String path = copyFiletoExternalStorage(R.raw.increible, "increible.mp3");
final Uri soundUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, soundUri);
startActivity(Intent.createChooser(share, "Compartiendo..."));
If anyone has any idea why this behaviour, I would appreciate it if you could share it :)
I need to improve some of these codes to be able to send audio files from my app through whatsapp

Related

GIF is getting shared as an image not animated - Android Java

I am trying to share an GIF programmatically, but it is getting shared as an image (not animated)
Code Snippet:
Intent shareIntent = new Intent();
shareIntent.setType("image/gif");//image/gif
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(myList.get(item)));
startActivity(Intent.createChooser(shareIntent,"Share gif"));
You are trying to share to which apps?
Some whatsapp builds don't support gif feature.
You can check this question here with some additional information: How to share GIF image in Whatsapp programmatically in Android?
There is this comment:
"This solution doesn't work with whatsapp but does work with all the other apps I specified above (namely Hangouts, Android Messages, Facebook Messenger)"
He is referring to this answer:
private void ShareGif() {
// replace the path and file name with your gif image path and name
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "temporary_file.gif";
File sharingGifFile = new File(baseDir, fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/gif");
Uri uri = Uri.fromFile(sharingGifFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share image"));
}
/*
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT,title + "\n\nLink : " + link );
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(sharePath));
shareIntent.setType("image/*");
startActivity(shareIntent);
*/
Hope it helps.

Share sound in my app

My app is a soundboard and I want it is possible that you can share the sounds. This is my code which does not work.
final Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("audio/mp3");
shareIntent.setPackage("com.whatsapp");
Uri recurso = Uri.parse("android.resource://" + PACKAGE_NAME + "/raw/" + R.raw.a);
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM, recurso);
startActivity(Intent.createChooser(shareIntent, getString(R.string.text1)));
But the sound can not be sent. What do you have to use as a path where does it have to lead?
pls help me

Android - Intent Sharing

How to detect from which (any) external app(or packagname) data was shared:
Via sharing an image/data (intent) from another app to my app(activity),
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}

Share intent in Android not sharing image to any app (after popup)

I am trying to share image by using share intent in Android. That showing list of installed apps after button click. But I select any one app it's not sharing. The opening app crashed or some app told sending this content type not support
My code:
Intent share = new Intent(Intent.ACTION_SEND);
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath() + "/");
dir.mkdirs();
Uri uri = Uri.parse(dir+"/img.jpg");
share.putExtra(Intent.EXTRA_STREAM,uri);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.setType("image/jpg");
startActivity(Intent.createChooser(share, "Share Image"));
and also I am giving permission for read and write external storage.
Log Cat:
I got this error repeatedly:
07-06 12:25:11.654: E/SurfaceFlinger(113): SurfaceFlinger translucent=1 isOpaque=0 isExternalDisplayLayer=0 isExternalBlockLayer0
Try This:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
startActivity(Intent.createChooser(shareIntent, "Share image using"));
http://developer.android.com/training/sharing/send.html

Android: Share Image intent not working with Facebook?

Hi I have the following code to share an image:
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
Uri uri = Uri.parse(getFilesDir() + File.separator + "myGoal.jpg");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
It works to share the image to Dropbox but if I pick the Facebook option, I get Facebook's status update dialog with no image attached and if I try to update my status with 'Test' it doesn't work. No errors. Just not working.
I know it's not the image because it uploads to my Dropbox properly and I can pull up the image and look at it.
Do I have to attach the image to the intent differently for it to work with Facebook?
Any ideas? I'm debugging on a physical device.
So I figured out the problem.
I was saving the picture to internal storage with getFilesDir() which put the picture into my apps sandbox and made inaccessible to the other apps.
I replaced my code with the following:
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/MyApp/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "myPic.png");
FileOutputStream fOut = new FileOutputStream(file);
screenshot.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
share.putExtra(Intent.EXTRA_TEXT, "My Image");
startActivity(Intent.createChooser(share, "Share Image"));
Works perfectly fine now.
What i have done to post image on facebook is instead of passing it directly i create a function and write it like this :
private void ShareWall(String message) {
Bundle parameters = new Bundle();
// share msg
parameters.putString("message", message);
// shre image which you have define above
parameters.putString("picture", postImage);
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("response: ", response);
if (response == null || response.equals("")) {
response.equals("");
showToast("No Response.");
} else {
showToast("Message has been posted to your walll!.");
}
finish();
} catch (Exception e) {
showToast("Message failed to posdt on wall.");
e.printStackTrace();
finish();
}
}
Hope this help you.
You can still share images (but not text) from your app to Facebook even if you are not using the Facebook SDK.
Just make sure that you use Uri.fromFile instead of Uri.parse and it will work:
DO NOT USE:
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathToFile));
USE:
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(pathToFile)));
Here is a solution without using external file write:
Drawable mDrawable = myImageView1.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image I want to share", null);
Uri uri = Uri.parse(path);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image"));
In this case, my image comes from an ImageView myImageView.

Categories