How to share video file allocated in Raw folder Android - android

In the app, I have a fragment where I play a videos. This videos are allocated in ../res/raw/<video.mp4>
To play the video I generate the path to it like:
this.videoPath = "android.resource://" + requireContext().packageName + "/" + args.videoId
where "args.videoId" is the id from the video generated using:
val packageName: String = context.packageName
val videoId = context.resources.getIdentifier(<video_name>, "raw", packageName) // video name for example "video1"
With all this stuff I can play the video using VideoView.
But the problem arrives when I have to share the video file to other apps like telegram/whatsapp... etc.
To do it, I launch an intent like:
val intent = Intent(Intent.ACTION_SEND)
val uri = Uri.parse(this.videoPath)
intent.apply {
type = "*/*"
putExtra(Intent.EXTRA_STREAM, uri)
}
startActivity(Intent.createChooser(intent, "Sharing Video:"))
The problem is that when I select the app to share I'm getting errorss.
For example in Telegram I get:
"Realpath failed for "dandroid.resource://com.mypackage.myapp/"
If I try to send via WhatsApp is sent something like a "file":
So , how can I share the mp4 file of the video?

Related

Getting null.bin file after sharing a video in whatsapp from my app

I have stored a video file in the android raw folder and want to share it through WhatsApp. But I am getting the file in WhatsApp as null.bin.
private void shareVideo(){
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/raw/video_name");
Intent videoshare = new Intent(Intent.ACTION_SEND);
videoshare.setType("*/*");
videoshare.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
videoshare.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(videoshare);
}

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

How to pick GIF files only

I want to pick Images(JPG, PNG) and GIF(gif) files separately. This works for most Gallery apps and file managers except Google Photos app. When I pick images or gifs using Google Photos app, all images are displayed i.e. JPG, PNG, GIF, BMP, etc. How can I tell Google Photos to allow picking only selected type of files?
Here's my code:
val contentPicker = activity.registerForActivityResult(
ActivityResultContracts.GetContent()
) { uri: Uri? ->
// Use Picked URI
}
val mime = when {
MediaType.isPhoto(mediaType) -> "image/*" // or image/jpg or image/png
MediaType.isGif(mediaType) -> "image/gif"
else -> throw RuntimeException("Wrong Media Type to pick: $mediaType")
}
contentPicker.launch(mime)
If there's another way besides this(maybe targeting Google Photos only), please share as an answer. I couldn't find any relevant question with proper answer. I want to use system apps only to pick content.
Edit (Nov 23, 2021)
Even if I use following intent to open Files app, a user can just go to Side Bar and select Photos app. Files app prevents picking gif/heic images. But, when Photos app is opened from within the Files app, user can select gif/heic images without any restriction.
fun getPickPhotoIntent(): Intent {
val pickIntent = Intent(Intent.ACTION_GET_CONTENT)
pickIntent.addCategory(Intent.CATEGORY_OPENABLE)
pickIntent.type = "image/jpg"
val mimeTypes = arrayOf("image/bmp", "image/jpeg", "image/png")
pickIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
pickIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
return pickIntent
}
fun getPickPhotoIntent(): Intent {
val pickIntent = Intent(com.google.android.apps.photos.picker.external.ExternalPickerActivity)//package name Used
pickIntent.addCategory(Intent.CATEGORY_OPENABLE)
pickIntent.type = "image/jpg"
val mimeTypes = arrayOf("image/bmp", "image/jpeg", "image/png")
pickIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
pickIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
return pickIntent
}

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

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

I want to make android application to send picture, message to instagram users using instagram API

I've tried several sources from Google and GitHub but didn't find any authentic source that could help me sending multiple pictures and posts automatically from my android gallery using the scheduler. Is anyone working with the Instagram API? If so, could you please give me some authentic source?
Instead of using the Instagram API, you can directly start an Intent to post an Image or Video to Instagram by opening a "Share with" dialog. This will require user interaction.
String type = "image/*";
String filename = "/myPhoto.jpg";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
private void createInstagramIntent(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"));
}
Code example taken from https://www.instagram.com/developer/mobile-sharing/android-intents/

Categories

Resources