I'm trying to play video files stored in android internal memory through intent. Files exist there but when i try to play them through intent, it give me the error "Media file not supported" or "can't play video" depending on device. I couldn't find where i'm wrong. here is my code
File mydir = activity.getDir("Videos", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, filename);
String videoResource = fileWithinMyDir.getPath();
Uri intentUri = Uri.parse(videoResource);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(intentUri, "video/mp4");
startActivity(intent);
I don't know where i'm wrong and what should i do. any help would be much appreciated. Thank you :)
Thank you so much friends for participation, your efforts are very much appreciated. I'v found the solution of my problem. There is need to set file read true before playing it.
fileWithinMyDir.setReadable(true, false);
Now here is complete code of intent to play mp4 video from internal storage of android.
File mydir = activity.getDir("Videos", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, headingsList.get(mPosition));
fileWithinMyDir.setReadable(true, false);
String videoResource = fileWithinMyDir.getPath();
Uri intentUri = Uri.fromFile(new File(videoResource));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(intentUri, "video/mp4");
startActivity(intent);
This code works for me:
Uri fileUri = Uri.fromFile(new File(String file_path));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, URLConnection.guessContentTypeFromName(fileUri.toString()));
startActivity(intent);
Related
how to get pdf from internal storage in android Using WebView or AnyConcept. i have to get pdf in an activity from internal storage.Can anyone of u guys let me know about this .Thank You.
String filename = null;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
startActivity(intent);
and give pdf filename
I am making an app in which I have to open default audio player. My code is as follows:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(songs.get(position));
//String introURI = "file:///sdcard/"+".mp3";
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
When the song is touched, the player gets open but it gives message that "this type of file is not supported".
The MediaPlayer class should be used when you want to implement your own media player. If you want to use an existing player, you'll have to launch the appropriate intent, for example:
Uri uri = Uri.parse("http://www.example.com/file.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
EDIT
How to get android local files uri
It will work. Try this
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);// path where your while is placed.For me like /storage/sdcard0/Media/audio/%2F1506580826442?alt=media&token=0e22f657-743c-4aed-9fed-48de69aced73.mp3
intent.setDataAndType(Uri.fromFile(file), "audio/*");
ActivityChatView.mContext.startActivity(intent);
How come every time i try to open a pdf file in my SDCARD using the following code, it doesn't actually open the pdf file itself, but goes into the menu of adobe reader? Is there anything wrong with my code?
Intent intent = new Intent();
File pdfFile = new File("/sdcard/sample.pdf");
Uri path = Uri.fromFile(pdfFile);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(path);
intent.setType("application/pdf");
intent.setPackage("com.adobe.reader");
startActivity(intent);
No, there's nothing wrong. You've set the type to pdf and specified the package as adobe.reader. What that does is fire off an intent to launch the pdf in adobe reader. There's no way to display a pdf directly in your app without using a library (or writing code yourself) to render PDFs and display them.
Note that if you only set the type and not the package, the system will find whatever app is available to display PDFs
EDIT
You can try something like
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File( filename );
intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
startActivity(intent);
or
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);
Here I am giving my pdf file name. You give your file name.
private static String FILE = Environment.getExternalStorageDirectory().getPath()+"/TestPDF.pdf";
File file = new File(FILE);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
I want to show user my apk file using intents, here is the code:
SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,fileName);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new
Intent(Intent.ACTION_VIEW,path);
startActivity(intent);
but it is giving me ActivityNotFoundException,I want to start the apk file so that user can install it.
Try this?
Uri path = Uri.fromFile(file);
Intent intent = new
Intent(Intent.ACTION_VIEW,path);
intent.setDataAndType(path, "application/vnd.android.package-archive");
startActivity(intent);
I am trying to create program that simply opens file on sdcard. I tried opening mp3, mp4, and apk - the code bellow always crashes unexpectedly.
String _path = "file:///sdcard/1.apk";
Uri outputFileUri = Uri.parse(_path);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Market links also crashes. But when I set _path="http://google.com" - browser opens normally. How can I make this code work?
If you're trying to install the apk, you need to use the following:
String fileName = Environment.getExternalStorageDirectory() + "/1.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);
If you're trying to launch the app (after installing), then:
Intent intent = new Intent();
intent.setClassName("com.pkg.addr", "com.pkg.addr.MainActivity");
startActivity(intent);
If you're trying to launch a player to play the mp3 file:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse("file:///sdcard/1.mp3");
intent.setDataAndType(data,"audio/mp3");
startActivity(intent);
Hope that helps.