Cannot start video player activity - java

I have a video in my /res/raw/ folder and I would like to start it using the native video player of the device.
This is my code:
String packageName = this.getPackageName();
Uri uri = Uri.parse("android.resource://" + packageName + "/raw/" + R.raw.my_video);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
startActivity(intent);
When I trie to start the video it crashes and I get this error in LogCat:
09-16 15:05:51.959: E/AndroidRuntime(23298): FATAL EXCEPTION: main
09-16 15:05:51.959: E/AndroidRuntime(23298): java.lang.IllegalStateException: Could not execute method of the activity
I tried Uri.parse(String) on a hosted mp4-file from a random webpage with the same result.
What is my problem?

Issue is you are using wrong raw folder name.
Uri uri = Uri.parse("android.resource://" + packageName + "/raw/" + R.raw.my_video);
Remove and use like this.
Uri uri = Uri.parse("android.resource://" + packageName + "/" + R.raw.my_video);

Hi please try below code hope it will help you
Uri intentUri = Uri.parse("android.resource://"+ packageName + "/" + R.raw.my_video);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(intentUri, "video/mp4");
startActivity(intent);

you add /raw extra in your URI path , Please make it
Uri uri = Uri.parse("android.resource://" + packageName + "/" + R.raw.my_video);

Related

trying open folder with intent

i want to open a specific folder from menu drawer in android app but when i run the app it open recent files folder.
this is my code
if (id == R.id.action_downloaded) {
if (PermissionUtilities.isPermissionGranted(mActivity, PermissionUtilities.SD_READ_WRITE_PERMISSIONS, PermissionUtilities.REQUEST_READ_WRITE_STORAGE_DOWNLOAD)) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory() + "/" + myfolder + "/");
intent.setDataAndType(uri, "application/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), 1000);
}
Put as extra INITIAL_URI on your intent an uri the user choosed before with ACTION_GET_CONTENT.

How to share externally Excel file stored in app's internal storage

I am trying to share the .xlsx file stored in App's internal storage.
when i tried below code it shows unable to load reasource.
public void share(String fileName, Context context)
{
Uri fileUri = Uri.parse(Environment.getDataDirectory() + getPackageName() + "/files/" + fileName);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
shareIntent.setType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
startActivity(Intent.createChooser(shareIntent, "Send to"));
}
any help will be appreciable

FileUriExposed exception error

im getting the following error in my logcat.
: android.os.FileUriExposedException: file:///storage/emulated/0/download/AllCast.apk exposed beyond app through Intent.getData()
it only happening on some devices ie marsh mellow and nougat.
this is where error occurs
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + newnamestring)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
ive tried adding this line of code
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
also tried this in my app class
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
can anybody tell me what im doing wrong. thanks
EDIT****
Ok guys im trying to get #CommonsWare answer to work i can get the file to download fin to the provider folder. but then my app crashes when it trys to handle the intent. here is my new log cat
05-07 16:25:35.784 8715-8715/com.example.harrops.h20droidapp2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: mypackagename, PID: 8715
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getName()' on a null object reference
at my package name.Casting_Apps$DownloadFileFromURL.onPostExecute(Casting_Apps.java:273)
which leads me to section of code
File newFile = null;
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = newFile.getName().substring(newFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(getBaseContext(), "my package name", newFile);
intent.setDataAndType(contentUri, type);
} else {
intent.setDataAndType(Uri.fromFile(newFile), type);
}
startActivityForResult(intent, ACTIVITY_VIEW_ATTACHMENT);
} catch (ActivityNotFoundException anfe) {
Toast.makeText(getBaseContext(), "No activity found to open this attachment.", Toast.LENGTH_LONG).show();
}
This is the answer i went for.. thanks #CommonsWare for the help
File toInstall = new File(appDirectory, appName + ".apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent)
} else {
Uri apkUri = Uri.fromFile(toInstall);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}

Can't send image in my application to external app as implicit intent

I am new to android i have read mant aricles on this from stackoverflow and other resources but i tried my best to get result
At time of sending i select gmail to send my image but its showing empty file cant be attached
i tried with different images in my package but i get the same response
Here is my code which i tried
Intent intent;
Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + getResources().getResourcePackageName(R.drawable.cursor)
+ '/' + getResources().getResourceTypeName(R.drawable.cursor) + '/' +
getResources().getResourceEntryName(R.drawable.cursor) );
intent=new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
chooser=Intent.createChooser(intent, "Select app to send");
startActivity(chooser);
Hope this helps:
BitmapFactory.Options options = new BitmapFactory.Options();
options.outMimeType = "image/jpeg";
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher, options);
String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Image Description", null);
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share Image"));

run intent with resource Android

It is necessary to start intent with an image resource.
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.ic_launcher);
Intent intent = new Intent(Intent.ACTION_VIEW, null);
intent.setData(path);
intent.setType("image/png");
startActivity(intent);
There is an error: "the gallery is closed"
Where is my problem?

Categories

Resources