How to play videos with phone's standard video player app - android

I want to play videos i have stored in the external storage with the phone's standard video Player app. I've tried using FileProvider, but I can't manage to pass the video to the player.
private void passVideo(String videoname){
File videoPath = new File(Environment.getExternalStorageDirectory(), "video_folder");
File newFile = new File(videoPath, videoname);
Uri path = FileProvider.getUriForFile(this, "com.example.provider", newFile);
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType(getContentResolver().getType(path))
.setStream(path)
.getIntent();
shareIntent.setData(path);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Open Video..."));
}
With this code I manage to get the Chooser for gmail, whatsapp and other social media platforms, but that's not what I want and they all say they can't handle the file format anyway. It also gives the option to play the video with VLC, but it instantly crashes.
I have tried every possible file format and none of them works.
Sorry if I'm missing something obvious, I am still a beginner.

ShareCompat.IntentBuilder is for ACTION_SEND, which is not the typical Intent action for playing a video. ACTION_VIEW would be more typical. So, try:
private void passVideo(String videoname){
File videoPath = new File(Environment.getExternalStorageDirectory(), "video_folder");
File newFile = new File(videoPath, videoname);
Uri uri = FileProvider.getUriForFile(this, "com.example.provider", newFile);
Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
viewIntent.setType(getContentResolver().getType(uri));
viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(viewIntent);
}

Related

How to share the Particular SQLITE audio file to Gmail in android?

I am working audio recording app, in which the audio is recorded and stored inside the app and that path is stored in SQLITE.
I need that audio file to be shared in whatsapp/gmail or saved in internal directory of mobile.
I try to share them using the Intent but in whatsapp it showed unsupported format.
I tried this code
private void Shareoption(List<RecordData> uploadlist)
{
RecordData reco2 = uploadlist.get(0);
String id3 = reco2.getId();
final RecordData recordData = DBcreatedata.getData(id3);
final File f;
locname = recordData.getRecordDataTitle();
f = new File(recordData.getRecordData());
Uri uri = Uri.parse(f.getAbsolutePath() );
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri.toString());
startActivity(Intent.createChooser(share, "Share Sound File"));
And I also searched about this and I came to know that first the audio file must be stored in internal directory before sharing. So I tried input output stream method, it did not work.
Can any one help me to share this audio file ?
By using this I think your problem will be solved
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri shareFileUri= Uri.parse(string);
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.setType("*/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, shareFileUri);
sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(sharingIntent, "Share Sound File"));

How to open DCIM/Camera folder in default Files app from intent

I am making a camera app which takes both front camera and back camera images/video and I do not want individual thumbnails on the Camera preview for each file.
I want to open "/storage/emulated/0/DCIM/Camera" folder using the Files app and further open the photo/video which is not possible with ACTION_GET_CONTENT as it selects the image and exits the Files app as tried here -
val intent = Intent(Intent.ACTION_GET_CONTENT)
val uri: Uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path.toString() + "/Camera")
intent.setDataAndType(uri, "*/*")
startActivity(Intent.createChooser(intent, "Open folder"))
I tried ACTION_VIEW too, but it is not specific to one folder and opens the gallery showing all media as tried here -
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.type = "image/*"
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
"image/*" shows images and videos too in the gallery for me which is good. When "*/*" is used we can use the Files app too but it opens the downloads folder.
One solution I found works only with ES Explorer as tried here -
val uri: Uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path.toString() + "/Camera")
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "resource/folder")
startActivity(intent)
This is due to "resource/folder" not supported leading to a crash. Changing "resource/folder" to "*/*" makes Files app open the downloads folder and Photos app to hang.
It seems gallery can do it via buckets, but it too is not universal.
I am not asking for much, just to display my Camera folder from where I can open and view any photo/video.
It was not possible if the user had not once selected a file using ACTION_GET_CONTENT was the opinion.
But now... try this code:
String scheme = "content://com.android.externalstorage.documents/document/primary%3APictures";
// String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";
Uri uri = Uri.parse(scheme);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri); // Added in API level 26
startActivityForResult(intent, 12345);
Toast.makeText(context, "Picker opened in:\n\n" + uri.toString(), Toast.LENGTH_LONG).show();
It works here and i'm pretty amazed.
To open the Files app starting with a certain folder you can use below code but only for Android 11 devices sadly.
//String scheme = "content://com.android.externalstorage.documents/document/primary%3APictures";
//String scheme = "content://com.android.externalstorage.documents/document/primary%3ADownload";
//String scheme = "content://com.android.externalstorage.documents/document/10F9-2E19%3ADownload";
String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";
Uri uri = Uri.parse(scheme);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "vnd.android.document/root");
startActivity(intent );
Toast.makeText(context, "Files app opening in:\n\n" + uri.toString(), Toast.LENGTH_LONG).show();

Video sent to whatsapp not playing

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");

How to play video mp4 using intent from my android app? This is possible?

I need to play a mp4 video in my app, bat I want to use intent. Is this possible?
private void startTrailer(){
Uri contentUri = Uri.parse("android.resource://" + pkgName + "/" +R.raw.v01_homem_ferro_3);
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setDataAndType( contentUri, "video/mp4" );
context.startActivity( intent );
}
Exception: No Activity found to handle Intent.
Somebody had this issue before. Check these links and see if they give you a good starting point to solve the problem:
Android intent for playing video?
How to play a video (.mp4) from assets or raw folder with the video intent?
Please try this, Here strMyVideo is my filepath,
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(strMyVideo));
intent.setDataAndType(Uri.parse(strMyVideo), "video/mp4");
activity.startActivity(intent);

Open PDF in Android

I would like to open a PDF file from my android application. I've searched how to do it in internet, and it seems very easy, but it doesn't work, at least in my mobile (Sony XPeria P).
File file = ....
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.fromFile (file));
intent.setType("application/pdf");
startActivity(intent);
When this code is executed, a window is opened asking to choose an application to show the PDF. When I choose the Adobe Reader, it's opened by no document is shown.
What I'm doing wrong?
Try this, Its working for me
//Method to open the pdf file in the PDF Viewer
public void OpenPDFFile() {
File pdfFile = new File(Environment.getExternalStorageDirectory(),"PdfFile.pdf");//File path
if (pdfFile.exists()) //Checking for the file is exist or not
{
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);//Staring the pdf viewer
}
}
The setType("application/pdf") function removes all the previous data.
If the targetSdkVersion >= 24 we need to use file provider to get the Uri. See this post for details: https://stackoverflow.com/a/38858040/8192914
So, the final code would look something like this:
Uri pathUri = FileProvider.getUriForFile(getBaseContext(), context.getApplicationContext().getPackageName() + ".provider", finalFile);
Intent pdfViewerIntent = new Intent(Intent.ACTION_VIEW);
pdfViewerIntent.setDataAndType(pathUri, "application/pdf");
pdfViewerIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
pdfViewerIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(pdfViewerIntent);

Categories

Resources