startActivity on "file://" links crashes - android

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.

Related

Automatically open Intent Chooser for the file Android

I want to open a file in android. What i want to do is if the file is of type Image then i want to open Intent Chooser which contains applications that can view the image, and if it is of video type, then open Intent Chooser with applications that can view videos. How can i achieve this?
I have found a solution. I am pasting it here so it may help other users.
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file), type);
context.startActivity(intent);
You should decide and know whether the file is video or image. You may do it by looking at the extension of the files.
After that you can open videos like this:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(result), "video/*");
startActivity(intent);
and images like this:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(result), "image/*");
startActivity(intent);
Android system will open the Intent Chooser automatically.

Open MS RD Client with own App and rdp-File

i have develope a programm for android to get rdp connections.
My problem is:
When i use ES File Explorer, i can open the rdp-file with "open with and choose RD Client" and it works perfectly, the app will show the login credentials.
In my program there is a button, when i click them it will open the RD Client app and it show a message: The rdp-file is not valid...
here is the code-fragment:
File file = new File("/sdcard1/SRV01.RDP");
Uri data = Uri.fromFile(file);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setDataAndType(data, "application/*");
startActivity(i);
and my alternative:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.microsoft.rdc.android");
Uri data = Uri.parse("file://" + file);
LaunchIntent.setData(data);
LaunchIntent.setType("rdp");
startActivity( LaunchIntent );
I didn't find the failure. I hope there is anyone who can help me.
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/test.rdp");
Uri data = Uri.fromFile(file);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setDataAndType(data, "application/rdp");
startActivity(i);

Android:open default audio player

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);

Opening pdf file programmatically is going into menu page?

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();
}
}

Mediaplayer exception

Intent intent1 = new Intent(
android.provider.MediaStore.INTENT_ACTION_MUSIC_PLAYER)
.setData(selectedImageUri);
i want to play a media file using android default media player but its not working in devices showing
ActivityNotFoundException
.can any one help me to correct it.i am stuck here
String extension = MimeTypeMap
.getFileExtensionFromUrl(selectedImagePath);
String mimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension);
Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
mediaIntent.setDataAndType(Uri.parse(selectedImagePath),
mimeType);
startActivity(mediaIntent);
I used this code and i got my output.
Maybe this will help you. The following is the piece which I use and it works fine. Pass the url to your default media Player and from there it will take care of it.
Uri myUri = Uri.parse( //your url);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(myUri, "audio/*");
startActivity(intent);

Categories

Resources