Does anyone know of an intent I can use to open a raw mp4 resource in the phone's video player? I tried this code:
Uri video = Uri.parse( "android.resource://" + getPackageName() + "/" + R.raw.video );
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(video, "video/*");
startActivity(intent);
This threw an ActivityNotFoundException, however this has worked for me when the URI was the sdcard and the video was located there. Does anyone know how I can use the above code with a resource file in my app?
Other apps can't access your resources. You'll need to create a ContentProvider, or store the data in a place accessible to anyone (either SD card, or in a public folder in your local storage).
Related
I have a file stored in my App's storage like this:
/storage/emulated/0/Android/data/com.example.myapp/files/App files/90210_John_s_Resume.pdf
the 90210 at the beginning is a file ID for internal purposes only, and I give the user the option to save this file to Downloads folder, where I remove the 90210 prefix. But I also provide the user to preview the file using ACTION_VIEW intent. This causes an issue where the file name in the previewed app still has the 90210 prefix. Is there a way to overcome this?
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri fileUri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", finalFile);//NO I18N
intent.setDataAndType(fileUri, URLConnection.guessContentTypeFromName(fileUri.toString()));
context.startActivity(intent);
I use the following code to open a pdf form. If I use Acrobat Reader it's not possible to write back to the original file. It always makes a copy which I only could guess but newer know for shure in my app.
I know that it has to be possible to edit the original because if I open a pdf from any file manager (e.g. the one from Asus) Adobe Reader edits it directly.
Xodo PDF allows direct edit. It even supports the correct ACTION_EDIT intent but I'm afraid that our users insist to use Adobe...
How can I edit the original with Adobe?
final File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS) + File.separator + "x.pdf");
if(file.exists())
{
file.setWritable(true,false);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri contentUri = FileProvider.getUriForFile(Objects.requireNonNull(getContext()),
BuildConfig.APPLICATION_ID + ".fileprovider", file);
intent.setDataAndType(contentUri,"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent = Intent.createChooser(intent, "Open File");
startActivity(intent);
}
At the moment even Xodo doesn't work anymore but I was lucky to realized that OneDrive-PDF-Viewer does.
I am trying to open "MyFolder" but it is opening recent files folder.
I have tried many techniques, nothing seems to work.
All are similar to this code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() +
File.separator + "MyFolder" + File.separator);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
Is it possible, if yes, how can I do it?
Any help would be appreciated.
First, there is no standard Intent action to "open" a filesystem directory on Android.
Second, the value that you pass to Uri.parse() needs to be a string form of a Uri, with a scheme. If you are going to create a Uri from a File, use Uri.fromFile(), rather than turning the File into a String and passing that String to Uri.parse().
Third, ACTION_GET_CONTENT does not take a Uri.
I have video that is locally stored in assets folder. Here is my code. I am getting error cannot play video. It is .mp4 file. It plays perfectly on my macbook.
Uri video = Uri.parse("file:///android_asset/video");
Intent intent = new Intent(Intent.ACTION_VIEW, video)
intent.setDataAndType(video, "video/*");
startActivity(Intent.createChooser(intent, "Play Video"));
I have also placing it in raw folder, In this case I am not getting ActiviyNotFoundException
Uri video = Uri.parse("file:///android.resource://" + getActivity().getPackageName() + "/" + R.raw.video);
The assets directory is private to your application; other applications cannot access it.
You will either need to provide access to the file using a FileProvider, or copy the file to a directory that other applications can access.
If you want to play from sdcard then the try this.
Uri vidFile = Uri.parse("link");
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
videoView.setVideoPath(path);
videoView.setMediaController(new MediaController(this));
videoView.start();
path can also be retrieved by MediaStore.Video.Media.DATA or you can type the path as /sdcard/videos
Also have a look at this tutorial
I have created a pdf file in my application and successfully stored that in sdCard, now i want to store the pdf not in my sdcard but in Internal storage as shown here but when i open an intent for pdf readers as:
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
Log.v("Path",file.getAbsolutePath()); // Path data/data/com.cloudchowk.his.patient/files/LIPID PROFILE1300000403.pdf
i.setDataAndType(uri, "application/pdf");
startActivity(i);
The PDF reader shows an error The document path is not valid.
I have tried this on two devices one was rooted and one was not, but it was not working on any of them. What should i do ? any ideas?
Get the path and create URI
File internalFile = getFileStreamPath("pdf path");
Uri file = Uri.fromFile(internalFile);
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(file, "application/pdf");
startActivity(i);
To make a file from internal storage available to other apps the cleanest approach is to use a content provider.
Luckily, Android comes with FileProvider – an implementation that might just serve your needs.
You don't need to implement code, you just need to specifiy the provider in your manifest.