How to share video URL to all apps in android (Whats app, Facebook etc)?
By applying below code, I am facing "File Format not supported error"?
enter code here
ContentValues content = new ContentValues(4);
content.put(MediaStore.Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, "http://www.demonuts.com/Demonuts/smallvideo.mp4");
ContentResolver resolver = getBaseContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("video/*");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"share:"));
First, the documentation states that the value for EXTRA_STREAM is supposed to be a Uri with a content: scheme, which means it points to a ContentProvider. Apps implementing ACTION_SEND might support other schemes, but they do not have to, and in particular they do not have to support http.
Second, use a real MIME type, not a wildcard. You are supplying the content; you should know its MIME type.
Third, as others pointed out in comments, http://www.demonuts.com/Demonuts/smallvideo.mp4 is not a filesystem path. It is a URL. Use Uri.parse("http://www.demonuts.com/Demonuts/smallvideo.mp4") to get that in the form of a Uri.
Related
I want to implement share video to whatsApp and other app feature using Android Intent system.
I have been looking for it for last 2 days but I did not get proper solution and some solutions I found on StackOverFlow, they no longer work I guess.
Whenever share Intent opens and I click on whatsApp and choose contact to share then it says file format is not supported
Below is my code:
ContentValues content = new ContentValues(4);
content.put(MediaStore.Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, "/storage/emulated/0/WhatsApp/Media/WhatsApp Video/VID-20210822-WA0002.mp4");
ContentResolver resolver = getBaseContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("video/*");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"share:"));
I'm trying to make an application with sharing of children that are inside the "raw" folder of the application, but I'm not getting it. The file is shared but does not have an .mp3 extension. I was not able to hear Windows Media Player, but I put a .mp3 extension manually. Does anybody have any idea how I do it to get .mp3 extension automatically? Transform / rename a URI into mp3 file.
The name of the song comes as it is registered in the file, I believe that if it was possible to rename adding the .mp3 in the end may work. But I'm not able to rename.
I am using this code:
Intent share;
Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/raw/name_musica");
share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri); share.setType("audio/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(share, "Enviar via: "));
The documentation for EXTRA_STREAM states that it is:
A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
android.resource: is not content:. There is no requirement for anything that responds to ACTION_SEND to know what to do with an android.resource: Uri in EXTRA_STREAM.
Copy the resource to the local filesystem and use FileProvider, so FileProvider can give you a content: Uri to use with EXTRA_STREAM. Or, write your own ContentProvider that serves the content directly from your raw resource, and use a content: Uri for your provider.
Also, do not use audio/*. Use the actual concrete MIME type of the content that you are sharing.
I'm with a problem... When I try to open the Gmail from an Intent, with a attached file, It shows to me in a Toast: "cant send empty file". My PDF file is inside in a folder in my app (/storage/emulated/0/Android/data/teste.com.br.cartaovisitateste/files/business.pdf), but I cant suck this file, to put in the email. Problably, the problem is with to localize the path, but I don't know how to solve this
This is my code:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("vnd.android.cursor.dir/email");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
i.putExtra(Intent.EXTRA_EMAIL, "wallacekingsdon#gmail.com");
// it does not work
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/storage/emulated/0/Android/data/digitalsa.com.br.cartaovisitaultragaz/files/business.pdf"));
// it does not work too
Uri.fromFile(new File(Environment.getDataDirectory().getAbsolutePath(), "business.pdf"));
i.putExtra(Intent.EXTRA_SUBJECT, String.valueOf(Hawk.get("registro_nome")).concat(" Business Card"));
startActivity(Intent.createChooser(i, "Enviando e-mail..."));
i.setType("vnd.android.cursor.dir/email");
The MIME type of a PDF file is application/pdf. The MIME type of an ACTION_SEND Intent is either the type used for EXTRA_STREAM or EXTRA_TEXT. In your case, you are using EXTRA_STREAM, and that appears to be attempting to point to a PDF file.
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/storage/emulated/0/Android/data/digitalsa.com.br.cartaovisitaultragaz/files/business.pdf"));
Do not hardcode paths. And do not pass things that are not Uri values to Uri.parse(). A Uri has a scheme, like https, file, or content.
Uri.fromFile(new File(Environment.getDataDirectory().getAbsolutePath(), "business.pdf"));
First, you are not putting that in an extra. The value is not being used.
Second, Uri.fromFile() will not work on Android 7.0+ in an Intent extra. You will get a FileUriExposedException. Use FileProvider to serve up the PDF, and use FileProvider.getUriForFile() to get the Uri to put into EXTRA_STREAM, at least on Android 7.0+ devices.
Suppose I'm selecting a file externally via:
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, GALLERY_REQUEST_CODE);
and in the result is returned via onActivityResult
Uri file = data.getData();
This can be in different formats. What is the convention here to determine if a content resolver query is needed? If the file URI is a file path file:/// or a app URI like //package.example/....
I know I could check if the uri has a file:/// prefix but that seems a bit hacky. I just want the file path.
What is the convention here to determine if a content resolver query is needed?
openInputStream() on ContentResolver handles both the file scheme and the content scheme.
I know I could check if the uri has a file:/// prefix but that seems a bit hacky
Um, well, Uri has a getScheme() method.
I just want the file path.
There is no "file path" for a content Uri, as there is no requirement that the content be stored as a file.
Just to compliment CommonsWare answer. You can examine the scheme of the URI, using, for example, getScheme() method, as mentioned in CommonsWare's answer. A content provider URI scheme is content://, while a file URI scheme is file://
This is my implicit intent to invoke image editing apps on the device:
startActivity(new Intent(Intent.ACTION_EDIT).setDataAndType(myUri,
getMimeType(myUri)).setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION));
And this is how I getMimeType:
public String getMimeType(Uri uri) {
String mimeType = null;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return mimeType;
}
For some apps it crashes to load:
On the app Sketch_Camera only an invisible page loads up and disables interaction with my app.
On the app AirBrush it loads the app but crashes with this message "Failed to load image".
Is it related to minimum sdk version as mine is 16?
I've tested this on minimum sdk version of 9 too and no change in result.
Is there anything else that I should add to this intent to work with all the apps?
I've tried putExtra too and it doesn't help:
.putExtra(Intent.ACTION_EDIT, myUri);
I've some gallery apps on my device and all of them launch Sketch_Camera and AirBrush without any problem.
What's happening here? I'm so confused after two days of struggling with this phenomena.
It's a file created from path of one media store file by querying MediaStore.Images.Media.EXTERNAL_CONTENT_URI
There is no guarantee that the other app has rights to this location, or even that your app has rights to this location. For example, the image could be on removable storage. Besides, the file Uri scheme is being banned for cross-app usage, anyway.
Use a content Uri instead. For example, in this sample app, I query MediaStore for videos. Given a Cursor named row positioned at a particular video, I generate the Uri for it via:
videoUri=
ContentUris.withAppendedId(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, row.getInt(row.getColumnIndex(MediaStore.Video.Media._ID)));
This Uri both works for my own purposes (e.g., hand to Picasso to get a thumbnail, hand to VideoView for playback) and for handing to third-party apps (e.g., ACTION_VIEW for playback).
Other than changing the base Uri to the one you queried against (MediaStore.Images.Media.EXTERNAL_CONTENT_URI), the same basic code should work for you.
Also, get rid of the flags from your Intent. Those are only for where the Intent points to your own ContentProvider, which is not the case in either your original code or with the Uri that you create from withAppendedId().