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));
Related
I want to send a video that is stored in my raw folder in the Android Studio project to WhatsApp. How can I do it?
public void shareVideoWhatsApp() {
String path = "android.resource://" + getPackageName() + "/" + R.raw.file_1;
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_MEDIA_SHARED);
intent.setType("text/plain");
// intent.putExtra(Intent.EXTRA_STREAM,uri);
intent.putExtra( Intent.EXTRA_TEXT, "Hello");
intent.setData(Uri.parse("http://api.whatsapp.com/send?phone="+"++9112345443423"));
startActivity(intent);
}
I've seen a lot of questions regarding setting the start Uri for the Intent.ACTION_OPEN_DOCUMENT_TREE but all of those require a Uri that comes from having used that folder picker before.
What I want to do is send my users directly to the Download folder when picking the folder but I do not know how to convert /storage/emulated/0/Download to a Uri that I can pass as an extra using DocumentsContract.EXTRA_INITIAL_URI.
Is there a way to convert any file path to a DocumentsContract style Uri?
Edit: Just to be clear, I'm talking about the uri passed here:
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
.setFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION
or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
)
.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri)
We manupulate INITIAL_URI of .createOpenDocumentTreeIntent().
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Intent intent = sm.getPrimaryStorageVolume().createOpenDocumentTreeIntent();
//String startDir = "Android";
//String startDir = "Download"; // Not choosable on an Android 11 device
//String startDir = "DCIM";
//String startDir = "DCIM/Camera"; // replace "/", "%2F"
String startDir = "DCIM%2FCamera";
Uri uri = intent.getParcelableExtra("android.provider.extra.INITIAL_URI");
String scheme = uri.toString();
Log.d(TAG, "INITIAL_URI scheme: " + scheme);
scheme = scheme.replace("/root/", "/document/");
scheme += "%3A" + startDir;
uri = Uri.parse(scheme);
intent.putExtra("android.provider.extra.INITIAL_URI", uri);
Log.d(TAG, "uri: " + uri.toString());
((Activity) context).startActivityForResult(intent, REQUEST_ACTION_OPEN_DOCUMENT_TREE);
return;
}
The logs will print something like:
INITIAL_URI scheme: content://com.android.externalstorage.documents/root/primary
uri: content://com.android.externalstorage.documents/document/primary%3ADownload
The easiest way to do it is:
for example, if i want the user to select MEDIA folder of WhatsApp
val i = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
val initial = Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsApp%2FMedia")
i.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initial)
startActivityForResult(i, SCOPED_REQUEST_WA)
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
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)))
:)
Hi All I want to play video in android from raw folder in native player .I able to play video from sd card and server url. But if my mp4 is in raw folder its fire exception. Can somebody help me.
My code is here.
Uri uri = Uri.parse("android.resource://" + getPackageName() + R.raw.sun);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);intent.setDataAndType(uri, "video/*");
startActivity(intent);
its working if i play using Video View but i don't want play in Video View.
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.sun);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);intent.setDataAndType(uri, "video/*");
startActivity(intent);
what you missed is simply the "/"
Hope this helps :)