In my app there is a feature where the user can share a video with the app of their choice. The code is fairly straightforward (where mediaPath is a variable of type String which is a path to a valid video):
File media = new File(mediaPath);
Uri uri = FileProvider.getUriForFile(context, getString(R.string.file_provider_authority), media);
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("video/*");
String title = getString(R.string.share_video_title);
Intent chooser = Intent.createChooser(share, title);
if (share.resolveActivity(context.getPackageManager()) != null) {
startActivity(chooser);
}
Sharing works perfectly on gmail (for example) and seems to work fine on whatsapp as well. It compresses the video and uploads it. The recipient gets the video and is able to see a thumbnail and download it. However they cannot play the video.
I finally found the solution is here
public void shareVideoWhatsApp() {
Uri uri = Uri.fromFile(v);
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,uri);
startActivity(videoshare);
}
Refrence
Are you try this:
String path = ""; //should be local path of downloaded video
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, path);
ContentResolver resolver = getApplicationContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("video/*");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Hey this is the video subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Hey this is the video text");
sharingIntent.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"Share Video");
Related
Ours is a video hosting portal where users can upload and earn from their videos based on the views they get. We have recently launched an Android App and trying to integrate Share button to each video. Here is the code what we have placed
Intent intent = new Intent();
try {
URL url = new URL("https://www.clipsnow.com/videos/images/thumbnails/230/10493.jpg");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_SEND);
intent.setData(Uri.parse("https://www.clipsnow.com"));
intent.putExtra(Intent.EXTRA_TEXT,msg);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, getImageUri(v.getContext(), image));
intent.setType("image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
v.getContext().startActivity(Intent.createChooser(intent, "Share Video"));
} catch (Exception e) {
e.printStackTrace();
}
When we share any video with this, only thumbnail image is getting shared along with the video title. But, we need the video URL will get shared and when user tap on the URL, user will be taken to our app.
How can we do that?
This worked with me. Give a try!
File videoFile = new File(filePath);
Uri videoURI = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? FileProvider.getUriForFile(mContext, mContext.getPackageName(), videoFile)
: Uri.fromFile(videoFile);
ShareCompat.IntentBuilder.from(getActivity())
.setStream(videoURI)
.setType("video/mp4")
.setChooserTitle("Share video...")
.startChooser();
You should download video first. Then you can share with using ACTION_SEND.
String path = ""; //should be local path of downloaded video
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, path);
ContentResolver resolver = getApplicationContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("video/*");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Hey this is the video subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Hey this is the video text");
sharingIntent.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"Share Video");
I guess, all the other solutions are obsolete. Here is the working solution for sharing the video to any platform (Youtube, Gmail, Hangout, Whatsapp etc),
startActivity(
Intent.createChooser(
Intent().setAction(Intent.ACTION_SEND)
.setType("video/*")
.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.putExtra(
Intent.EXTRA_STREAM,
getVideoContentUri(this, File(currentVideo.videoPath))
), resources.getString(R.string.share_video)
)
)
Below is the getVideoContentUri method,
/**
* Return the URI for a file. This URI is used for
* sharing of video.
* NOTE: You cannot share a file by file path.
*
* #param context Context
* #param videoFile File
* #return Uri?
*/
fun getVideoContentUri(context: Context, videoFile: File): Uri? {
var uri: Uri? = null
val filePath = videoFile.absolutePath
val cursor = context.contentResolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
arrayOf(MediaStore.Video.Media._ID),
MediaStore.Video.Media.DATA + "=? ",
arrayOf(filePath), null)
if (cursor != null && cursor.moveToFirst()) {
val id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID))
val baseUri = Uri.parse("content://media/external/video/media")
uri = Uri.withAppendedPath(baseUri, "" + id)
} else if (videoFile.exists()) {
val values = ContentValues()
values.put(MediaStore.Video.Media.DATA, filePath)
uri = context.contentResolver.insert(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values)
}
closeCursor(cursor)
return uri
}
To send the video in android 10 and above use this. I used this to send the video from local storage to WhatsApp.
public void shareVideo(String filepath)
{
Intent shareintent=new Intent("android.intent.action.SEND");
shareintent.setType("video/mp4");
shareintent.putExtra("android.intent.extra.STREAM",
Uri.parse(filepath));
startActivity(Intent.createChooser(shareintent,"share"));
}
I am trying to share a video that is being created and stored on external sdcard whose path has been obtained by.
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath()
I am using SEND_INTENT as follows:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("video/mp4");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"My Text");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(video_path));
startActivityForResult(Intent.createChooser(shareIntent, "Share Your Video"),SHARE_INTENT);
Problem:
While I share through gmail, it shows me compose window with video attached. But no size being shown of the video and when you either send or cancel the window, gmail will crash with inputstream NPE on contentresolver.
In case of youtube, it says you cannot upload videos from cloud service, my video clearly resides on the device.
In case of facebook, it is silently discarded. This works fine with wassup. :-)
Any ideas how to get this to work?
EDIT:
Video Path:
/storage/emulated/0/Movies/MyFolder/my-video_1378253389208.mp4
UPDATE
By adding file:/// suffix, gmail and facebook works fine.
Youtube is still cribbing about "Videos cannot be uploaded from cloud services".
The reason Youtube fails and others work is because it checks to see if it is a media file. It only thinks it is a media file if it has been scanned. Run the code below and it should work. It will also show up in the gallery. How to upload a temp file I do not know.
void publishScan() {
//mVidFnam: "/mnt/sdcard/DCIM/foo/vid_20131001_123738.3gp" (or such)
MediaScannerConnection.scanFile(this, new String[] { mVidFnam }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.d(TAG, "onScanCompleted uri " + uri);
}
});
}
Based on Johan vdH's answer, the following code works on many sharing apps including Youtube, Facebook, WhatsApp and so on.
Path should be absolute path of video file. for eg. "/mnt/sdcard/DCIM/foo/vid_20131001_123738.3gp"
public void shareVideo(final String title, String path) {
MediaScannerConnection.scanFile(getActivity(), new String[] { path },
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("video/*");
shareIntent.putExtra(
android.content.Intent.EXTRA_SUBJECT, title);
shareIntent.putExtra(
android.content.Intent.EXTRA_TITLE, title);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(Intent.createChooser(shareIntent,
getString(R.string.str_share_this_video)));
}
});
}
In addition to Johan vdH's answer, the Uri used in
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
Must be the one obtained from
public void onScanCompleted(String path, Uri uri)
Getting the Uri by
Uri uri = Uri.fromFile(new File(video_path));
Will not work. Youtube seems to like content:// but not file://
The best and easy method method to Share video on youtube, Use Provider to get Video Uri in Devices with Api >=24
private void VideoShareOnYoutube(String videoPath) {
try {
String csYoutubePackage = "com.google.android.youtube";
Intent intent = getPackageManager().getLaunchIntentForPackage(csYoutubePackage);
if (intent != null && !videoPath.isEmpty()) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage(csYoutubePackage);
Uri contentUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
contentUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.name.provider", new File(videoPath));
}
else
{
contentUri = Uri.fromFile(new File(videoPath));
}
share.setType("video/*");
share.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(share);
} else {
Common.showToast("Youtube app not installed!", activty);
}
} catch (Exception e) {
Log.e(TAG, "Youtubeexeption() :" + e.getMessage());
}
}
with this code you can try.
public void makeVideo(String nameVideo){
String type = "video/*";
// /storage/emulated/0/nameVideo.mp4
// if you have other path is necessary you change the path
String mediaPath = Environment.getExternalStorageDirectory() + File.separator + nameVideo;
createIntent(type, mediaPath);
}
private void createIntent(String type, String mediaPath) {
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
more information https://instagram.com/developer/mobile-sharing/android-intents/
Just a heads up as this caught me out when trying to share a video to Facebook via Intent Chooser that worked fine with Gmail and a few other Intents -
If there are spaces or other characters that get encoded to %20 for space for example, Facebook won't find the video if you try to play it.
I was using Video - 27 Apr 2016 - 16:59 pm.mp4 for instance, and spaces and colons were replaced, breaking the link inside Facebook. A thumbnail of the video would show when composing the share in Facebook, so I thought it was OK, but if you clicked to play it, it would say the file could not be found / played.
No such problem with Gmail, it sent and was playable once retrieved from email.
Here is the working solution for sharing videos, (code is in kotlin)
startActivity(
Intent.createChooser(
Intent().setAction(Intent.ACTION_SEND)
.setType("video/*")
.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.putExtra(
Intent.EXTRA_STREAM,
getVideoContentUri(this, File(currentVideo.videoPath))
), resources.getString(R.string.share_video)
)
)
don't forget to add the following method,
/**
* Return the URI for a file. This URI is used for
* sharing of video.
* NOTE: You cannot share a file by file path.
*
* #param context Context
* #param videoFile File
* #return Uri?
*/
fun getVideoContentUri(context: Context, videoFile: File): Uri? {
var uri: Uri? = null
val filePath = videoFile.absolutePath
val cursor = context.contentResolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
arrayOf(MediaStore.Video.Media._ID),
MediaStore.Video.Media.DATA + "=? ",
arrayOf(filePath), null)
if (cursor != null && cursor.moveToFirst()) {
val id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID))
val baseUri = Uri.parse("content://media/external/video/media")
uri = Uri.withAppendedPath(baseUri, "" + id)
} else if (videoFile.exists()) {
val values = ContentValues()
values.put(MediaStore.Video.Media.DATA, filePath)
uri = context.contentResolver.insert(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values)
}
closeCursor(cursor)
return uri
}
Happy Codng :)
I want to upload video on you-tube and i am uploading video using this code but the problem is that whenever i click the button from my app then i have to select you-tube from list view of email,Bluetooth,you-tube,etc..
So i want the permanent solution for this that whenever i click a button from my app it automatically moves to you-tube without showing list view.
So, this is my code which i used to upload video over you-tube.
**ContentValues content = new ContentValues(4);
content.put(Video.VideoColumns.TITLE, "My Test");
content.put(Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, outputFile);
ContentResolver resolver = getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,content);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("video/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share using"));**
This might help you,
String url = "http://youtu.be/mo1DmO9JH3Y"; // Your Url here..
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://"+url));
activity.startActivity(intent);
I am trying to upload a video to youtube using the intent.
The way I have it set is described here
Here's snippet:
ContentValues content = new ContentValues(4);
content.put(Video.VideoColumns.TITLE, "My Test");
content.put(Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, outputFile);
ContentResolver resolver = getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
content);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("video/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share using"));
I would like to have the title field already set.
Otherwise, if the user does not manually set the title field the video uploaded will have the title of the video filename.
Go through the following link
http://diablo-urban-o.com/post/9929911420/uploading-videos-to-youtube-from-android-part-ii
and try the following code too.
ContentValues content = new ContentValues(4);
content.put(Video.VideoColumns.TITLE, "Test");
content.put(Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, "/sdcard/myvideo.mp4");
ContentResolver resolver = getContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
Hope this will help you...
Here is a demo application that does want you want . Use the ytUploader.setTitle("Android Test"); to set whatever title you want.
I have a problems, when I'm trying to share files via Bluetooth, Dropbox, etc.
Here's my code:
Intent intent = new Intent(Intent.ACTION_SEND);
File file = new File(opt.getPath());
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
intent.setType(mimetype);
String path = "file://" + file.getAbsolutePath();
intent.putExtra(Intent.EXTRA_STREAM, path);
startActivity(Intent.createChooser(intent, "Choose File"));
For example, when I select an image, getFileExtensionFromUrl returns empty string.
And when mimetype is correct(application/pdf for example), I also can't share file(I've getting message "Unsupported file type"). What's wrong am I doing?
Update
I partially solved this problem by myself.
Here's the code:
Intent intent = new Intent(Intent.ACTION_SEND);
File file = new File(opt.getPath());
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(file.getAbsolutePath());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
intent.setType(mimetype);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(intent, "Choose File"));
But getFileExtensionFromUrl function for example still returns empty string for video(tests on *.wmv)
Update. Solved that promlem by using this code:
int dotPos = file.getName().lastIndexOf(".")+1;
String ext = file.getName().substring(dotPos);
and removing
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(file.getAbsolutePath());