Kotlin - Share audio file from raw resource - android

I have a soundboard app for android and am trying to make users be able to share the sounds in the app via messenger, gmail etc. This is the code I've tried to use for that purpose:
Fragment:
val uri = SoundProvider.getUri(4,(activity as MainActivity).packageName)
val share = Intent(Intent.ACTION_SEND)
share.type = "audio/*"
share.putExtra(Intent.EXTRA_STREAM, uri)
startActivity(Intent.createChooser(share, "Share Sound File"))
getUri function:
fun getUri(id: Int, packageName: String):Uri{
val uri = Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE
+ File.pathSeparator + File.separator + File.separator
+ packageName
+ File.separator
+ R.raw.random_sound
)
return uri
}
Unfortunately, this code doesn't seem to work, when I click on one of the share options in the app ( for example gmail), it just opens a blank email with no attachments. Similarly with other apps. Does anyone happen to know how to make this work?

I think this is the right way means using File.pathSeparator 2 times and not 3 times consecutively:
ContentResolver.SCHEME_ANDROID_RESOURCE
+ File.pathSeparator + File.separator
+ packageName
+ File.separator
+ R.raw.random_sound

Related

How to share video file allocated in Raw folder 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?

android studio share CSV file request contain no data

I want to share my csv file to any action like Bluetooth, send to email ,etc
final String filename = Environment.getExternalStorageDirectory() + "/Folder" + "/" + "mycsv.csv";
Intent sharingIntent = new Intent();
sharingIntent.setAction(Intent.ACTION_SEND);
sharingIntent.putExtra(Intent.EXTRA_STREAM, filename);
sharingIntent.setType("text/comma_separated_values/csv");
startActivity(Intent.createChooser(sharingIntent, "share file with"));
the output will be no request containt no data
I think that the filename requires a file:// prefix. Android, for sharing any types of file requires a universal identifier or a Uri.
final String fileUriString = "file://" + Environment.getExternalStorageDirectory() + "/Folder" + "/" + "mycsv.csv";
Also, change the type to text/csv.
Intent sharingIntent = new Intent();
sharingIntent.setAction(Intent.ACTION_SEND);
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse( fileUriString ) ) ;
sharingIntent.setType("text/csv");
startActivity(Intent.createChooser(sharingIntent, "share file with"));
See this answer for more.

Share a resource image with intents

I'm trying to share an image/jpg stored in the raw resource folder of my application, but the Intent seems not to find the image resource and sends nothing.
Here is my code for sending (in Kotlin):
val current = filePaths!![mViewPager!!.currentItem]
val uri = Uri.parse("android.resource://" + getPackageName() + "/" + current.resourceId)
val shareIntent : Intent = Intent()
shareIntent.setAction(Intent.ACTION_SEND)
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.setType("image/*")
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)))
I also tried to send the Uri this way:
val uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + File.separator + getPackageName() + "/raw/" + filename)
but it doesn't work either. Can somebody help me?
The Uri passed via EXTRA_STREAM needs to be a content Uri. And while some apps supporting ACTION_SNED are more flexible, few will handle the almost-completely-unused android.resource scheme.
Implement a ContentProvider to serve your content, and use a Uri for that content in your Intent. Also, use a concrete MIME type in your Intent — this is your content, so you know what the MIME type is.
After 3 days of headaches i finally solved... what i've done was just save the image resource and then serve it:
val current = filePaths!![mViewPager!!.currentItem]
val imagePath = File(Environment.getExternalStorageDirectory(), "_temp")
if(!imagePath.exists())
imagePath.mkdirs()
val imageToShare = File(imagePath, "share.jpeg")
if(imageToShare.exists())
imageToShare.delete()
imageToShare.createNewFile()
val out = FileOutputStream(imageToShare)
val imageToSave = utils.createBitmap(current.resourceId)
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out)
out.flush()
out.close()
val uri = Uri.fromFile(imageToShare)
val shareIntent : Intent = Intent()
shareIntent.setAction(Intent.ACTION_SEND)
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.setType("image/jpeg")
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)))
:)

How to play the video from raw folder using intent?

I am getting the exception as:
Android.Content.ActivityNotFoundException: No Activity found to
handle Intent.
var video = Android.Net.Uri.Parse(#"android.resource://" +
Forms.Context.PackageName + "/Raw/" + Resource.Raw.sampleVideo);
Intent intent = new Intent(Intent.ActionView, video);
intent.SetDataAndType(video,"video/*");
Forms.Context.StartActivity(intent);
Chech whether you have declared your intent in AndroidManifest file. For playing video you can check this url https://stackoverflow.com/a/16020402/5376678
get the uri from raw folder in xamarin.android
Here is what you what :
string rawPath = "android.resource://" + PackageName + "/" + Resource.Raw.audio;
Android.Net.Uri uri = Android.Net.Uri.Parse((rawPath));

Social sharing on mobile

On a website, one can use a social sharing javascript library like addthis in order to propose share buttons to the user without having to program everything from scratch.
Do you know any library doing the same sort of thing directly inside an android application ?
pathToPicture in previous answer is vague. It should be an Uri.
See Android docs
More elaborate example:
String path = "/mnt/sdcard/dir1/sample_1.jpg";
Intent share = new Intent(Intent.ACTION_SEND);
MimeTypeMap map = MimeTypeMap.getSingleton(); //mapping from extension to mimetype
String ext = path.substring(path.lastIndexOf('.') + 1);
String mime = map.getMimeTypeFromExtension(ext);
share.setType(mime); // might be text, sound, whatever
Uri uri = Uri.fromFile(new File(path));
share.putExtra(Intent.EXTRA_STREAM,uri);//using a string here didnt work for me
Log.d(TAG, "share " + uri + " ext:" + ext + " mime:" + mime);
startActivity(Intent.createChooser(share, "share"));
On Android we have Intents for this.
If you like to give the user an opportunity to share something, you can fire up an intent like this for example:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg") // might be text, sound, whatever
share.putExtra(Intent.EXTRA_STREAM, pathToPicture);
startActivity(Intent.createChooser(share, "share"));

Categories

Resources