I'm using this code for some years now and it worked fine:
final Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.setType("audio/mpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM,
SoundProvider.getUriForSound(getContext(), sound));
getActivity()
.startActivity(Intent.createChooser(sharingIntent,
getContext().getString(R.string.share)));
My SoundProvider generates a URI that starts with content:// which is picked up by a FileProvider (actually the same SoundProvider). This provider reads an audio file from my raw folder.
The sounds was playable directly in WhatsApp (and not a generic file) and shown with the correct title from the ID3 tags.
This has worked flawlessly and still does with Telegram/Dropbox etc. but up until a recent WhatsApp update from a few months ago it fails with the message "Sharing failed please try again".
Is anyone aware of any changes made by WhatsApp and has encountered something similar?
Try this:
Uri uri = Uri.parse(audioPath);
Intent shareIntent = new Intent();
shareIntent.setType("audio/*");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
I had to work around this by copying the sounds to the external-files-dir.
I don't know why whatsapp suddenly doesn't accept files from the raw directory served by a FileProvider anymore while other apps still do without any problems.
Related
I am trying to share files from my app to another using the android app picker but after choosing the recieving app, say, whatsapp I get a "format not compatible" error once inside the whatsapp app.
The file I intend to share is always located in the DOWNLOADS folder.
My code for sharing is:
private void share() {
Uri uri = FileProvider.getUriForFile(getContext(), getContext().getResources().getString(R.string.file_provider_authority),
new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/" + fileToShare.getFileName()));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, fileToShare.getFileName()));
}
I know I need to use FileProviders because I'm targeting above V24 and believe this is the right way to do it but something must be missing because I keep getting this error.
Can you tell me what am I doing wrong so i can fix my code? Thank you!
I want to share the audio file in android using but didn't found any working solution.
In My app the app records and save the audio file named as audio_1.wav each time in the app directory and then provide the option for sharing it. So always it share the file with same name(audio_1.wav).
Also checked that it is creating the file correctly.
I tried to share the audio file with below code;
File f = new File(sharePath);
Uri uri = Uri.parse(f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));
But while sharing and selecting any app it show unable to attach File.
Also Used FileProvider Class for Nougat Devices but it didn't worked as well.
Checked multiple SO Link as well:
Sharing an audio file
Picking up an audio file android
With regards to the code that you have:
Replace Uri uri = Uri.parse(f.getAbsolutePath()); with Uri uri = Uri.fromFile(f);, to get a valid Uri
Use audio/wav, not audio/*, for the MIME type
Your code will crash on Android 7.0+ with a FileUriExposedException. Use FileProvider for that, where you use <external-path> in your metadata XML resource and add the FLAG_GRANT_READ_URI_PERMISSION flag to your Intent. See the documentation for more.
I have implemented the following code to share video:
// Copy video file to Fileprovider directory.
final String destFile = …
// Build FileProvider uri.
final Uri uri = FileProvider.getUriForFile(activity, AUTHORITY, destFile)
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setType("video/*");
Log.d(TAG, "Sharing " + sendIntent.getType() + " for " + uri);
String title = "Share this content with";
Intent chooser = Intent.createChooser(sendIntent, title);
if (null != sendIntent.resolveActivity(activity.getPackageManager())) {
activity.startActivity(chooser);
}
This code works and shows the chooser. When I select a messaging app like Telegram, I get to pick the destination conversation, but the video is sent as a file attachment, meaning that users only see a document icon and need to download it and open externally. However, the same video, using the OS system gallery, will open Telegram into their video editor where they can cut/edit the media and when sent will be visible inline in the conversation.
What am I missing to get the same behaviour? Looking through Android git repositories I don't see anything different from this to share content, so I don't know what I'm missing. The log I get with this code looks like
Sharing video/* for content://com.app.android.fileprovider/share/video-a.mp4
So not only does it have the mime type but also the file extension could help. When I change the code to use a different mime type for images then I get the same behaviour, where the images get inlined into Telegram's chat.
For some reason the culprit was the FileProvider. Once I removed the FileProvider and passed in directly file:// URIs everything works.
I am writing an Android application to upload a mp4 format video from sdcard to youtube using sharing Intent. It ran perfectly at the first time when I installed to my phone. However, after I restart the app, I cannot share it again. If i share to whatsapp, "sharing fail" apear. If I click youtube, I got "no media uri(s)" message on logcat. Reinstalling on same phone or restarting the phone cannot solve the problem.
Even when I install the app in same code to other android device, it only runs perfectly once. When i restart the app, same problem happened.
I got the code from this website:
Trouble with youtube upload
And this is my code:
findViewById(R.id.button2).setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("video/*");
ContentValues content = new ContentValues(4);
content.put(Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, Environment.getExternalStorageDirectory().getAbsolutePath()+"/myVideo/myVideoTemp.mp4");
ContentResolver resolver = getBaseContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_TITLE,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"Share video"));
}
});
p.s. there is no error in red color on logcat
Problem solved after adding sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Try the following to invoke the "share" applications:
//Use the URI that points to the video file, in my case, the video file is in local storage (sd card)
Uri fileUri = Uri.fromFile(videoFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
//Use the setDataAndType method to provide the URI and MIME type to the file
//Please note, you need to use setDataAndType method to make it work
intent.setDataAndType(fileUri,URLConnection.guessContentTypeFromName(fileUri.toString()));
startActivity(intent);
I have a blog to show how you can invoke video players from your app with a video file stored locally. It may help:
http://software.intel.com/en-us/blogs/2014/03/20/video-playing-with-android-media-player
I have this code to send an emai with an audio attachment that is coming from the raw folder:
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("Audio/basic");
i.putExtra(Intent.EXTRA_SUBJECT, "mySubject");
i.putExtra(Intent.EXTRA_TEXT, "myBody");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://[my_package]/raw/sound"));
startActivity(i);
This code works fine.
I tried to adapt the same code so that I can send MMS message with audio attachment from the raw folder.
I came up with this:
String uri= "mmsto:";
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
i.putExtra("sms_body", "myBody");
i.putExtra("compose_mode", true);
i.putExtra(Intent.EXTRA_STREAM, Uri.parse"android.resource://[my_package]/raw/sound"));
startActivity(i);
The code opens an SMS application. However, there isn't any attachment to the message !
Am I doing it right? Please help me with that.
To those who might be interested:
MMS Functionality is a bit unreliable, not well-documented feature in Android. So, the existed solutions are supposed to work but they will not work all the time on all the devices. You can't depend on them, yet.