How to set duration to a video - android - android

I am trying to save a 10sec long mp4 file in the gallery (the goal is to upload the vid to facebook).
The log says to me that the file duration is 10sec long - as he should be, but when I'm trying to upload it to Facebook (manually), it appears like the video is 0sec long and I can't upload it.
(if I'm watching the video on the phone - its works normally, just when I trying to upload it t FB its happens)
private void addVideoGallery( File file ) {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.VideoColumns.DURATION,10000);
values.put(MediaStore.Video.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
Log.d("dur",""+values.getAsLong(MediaStore.Video.VideoColumns.DURATION));
getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
I just want to know if I'm setting the duration right (the actual duration is fixed on 10sec), or I should change/update anything?

I've found the solution, i just used the MediaScanner like this:
Intent intent =
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
For further information: http://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/

Related

What is the proper way to send a video background asset and an image sticker layer to instagram in Android?

I want to share a video to instagram from my app, using my app logo as a sticker. I have been following this documentation, but the results is that everytime I try to share, instagram opens, shows me my video and the sticker, it freezes for about a second, and then the sticker dissapears, only leaving me with my video and no sticker to be seen, even after posting said video.
I have also read this question, where they stated that not being able to share a sticker with a video was a bug with android, but it should now be fixed. The image Im using for the sticker is 640x480 which is the recommended size according to documentation so I dont think is the image size either. I can only think Im sharing it wrong.
Here is my code:
Uri stickerAssetUri = Uri.parse("android.resource://" + R.class.getPackage().getName() + "/drawable/" + R.drawable.app_logo_sticker);
File media = new File(localFilePath);
Uri backgroundAssetUri = FileProvider.getUriForFile(getActivity(), getString(R.string.file_provider), media);
Intent intent = new Intent("com.instagram.share.ADD_TO_STORY");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(backgroundAssetUri, "video/mp4");
intent.putExtra("interactive_asset_uri", stickerAssetUri);
getActivity().grantUriPermission(
"com.instagram.android", stickerAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (getActivity().getPackageManager().resolveActivity(intent, 0) != null) {
getActivity().startActivityForResult(intent, 0);
}
The problem was the video file I was using for testing. The max duration for a background asset is 20 seconds, and while the file I was using was 20 seconds according the videoview, retrieving the actual duration of the video gave me 20333 milis which is over the limit specified in the documentation. I used a different file that actually meets the criteria and it works.

"Unable to play video, Sorry this video cannot be played" when trying to display video from camera intent

I am having a weird problem. (I am new to android) In my app I have an intent to launch the video camera as well as one to launch the camera. Now what seems to happen is when I click on the image button to display image, it then display the images. But when trying to display video it says "unable to play video". So I thought there was something wrong with my phone so I restarted my phone and it then seemed to play. So when testing again it doesn't.
So I know there is nothing wrong with my code, but can't seem to display the video after it has been taken. (and only plays after a reboot of the device)
Could someone please help me?
Thanks
EDIT
To take the video I use:
Intent imageIntent = new Intent (android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
timeStamp = new SimpleDateFormat("ddMMMyyyy_HH:mm:ss").format(new Date());
File videosFolder = new File(Environment.getExternalStorageDirectory(), "Cool Videos");
videosFolder.mkdirs();
image = new File(imagesFolder.getPath(), "Cvidoes_" + timeStamp + ".mp4");
fUri = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(imageIntent, TAKE_PICTURE);
And then in my "display full screen"
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(temp_file),getMimeType(temp_file.getAbsolutePath()));
startActivity(intent);
But the weird thing is worked before I recently updated to android version 5.0.
EDIT 2
Okay I have solved the issue.
I have pretty much nothing in my "OnActivityResult" section, but added this and all is working again:
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{image.getAbsolutePath()},
null,
new OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.v("VideoScan",
"file " + path + " was scanned seccessfully: " + uri);
}
});
So why since the update it stopped working and now have to add this?
So why since the update it stopped working and now have to add this?
Strictly speaking, we can't really answer this definitively.
What you added was code to arrange to have your video be indexed by the MediaStore. A video player receiving a file:/// Uri — as you are using in your ACTION_VIEW Intent — should not really care about MediaStore. Apparently, yours does, or does after your device was upgraded to Android 5.0. IMHO, that's a bug in the video player.
Now, in general, if you want the user to be able to work with the video separately from your app, you need to get it indexed by the MediaStore. That will happen automatically... eventually. Your code addition makes it happen much more quickly, which is generally a good idea.
So, I'd look at it more that you tripped over a bug in the video player, whose fix happened to be some code that you probably needed anyway.
In other words, it's just another day at the office... :-)

How to make images I download within my app show in default gallery?

I made an application that has as one of it's features file sharing, and the files sent may be anything. However, Images and Videos sent are not showing up on the default Gallery app using the com.androidquery.AQuery download. Is there a step I'm missing that would mark the file as media or something like that? Because I thought you only needed to mark file as NOT media on Android when you really don't want them to show.
After file downloading you need to execute
private void addImageGallery(File file) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
To register your file in android MediaStore where gallery takes data about stored media files
After some research based on Sone's answer, I got this code I needed to insert after the download:
Uri uri = Uri.fromFile(new File(downloadFilePath)); //Insert your file path here
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uri);
getApplicationContext().sendBroadcast(mediaScanIntent);
As he said, I needed to register my file in android MediaStore, and this code does that no matter what type of media is downloaded by forcing the scanner to go over the recently downloaded file. Hope it helps anyone else needing this.

Upload video from Android app to Youtube, Facebook, Gmail using SEND intent

[EDIT] I'm working on Android Lollipop. I want to share (upload) my video (.mp4) from sdcard to Facebook, Youtube,... using intent SEND. My problem is that: when I upload video first time, everything works perfectly. But if I try to upload this video again, nothing is attached. (For example: first time: I upload video to Facebook successfully. Then I try to upload it to Facebook again, video is not attached). Here is my code now:
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, "/sdcard/rec5.mp4");
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);
intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title");
startActivity(Intent.createChooser(intent, "Share using"));
Can anybody tell me what did I miss in my code?

Android Sharing Intent cannot start

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

Categories

Resources