how to show app list to open a file - android

I'm trying to give the user an overview over the apps which can be used to open the selected file. The following code doesn't work properly:
String ext = MimeTypeMap.getFileExtensionFromUrl(file2open);
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(file2open), mime);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
If a .txt-file is selected, a list with two apps is shown. But no one of them is the right one. If a .pdf-file is selected, on my phone, a pdf-reader will be launched but only the app. The file is not getting opened. How can I show a list with all apps installed on the device to make the user select one, for example, if a file name doesn't contain an extension?

Solved: It was
intent.setDataAndType(Uri.fromFile(f), mime);
Instead of
intent.setDataAndType(Uri.parse(file2open), mime);

Related

How can i do a menu chooser in android

I want to do a menu where u can choose the audio apps that are installed in your phone (like Youtube, Music, Skype...) so I donĀ“t know the exactly name of this type of menu (if this have a special name).
This should be what you are searching for: https://developer.android.com/training/basics/intents/sending.html
Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.address");
if (intent != null) {
startActivity(launchIntent);
}
Replace com.package.address with the package name of the application you want to open.
If I understand, you would to send file to another app based on type of this file.
This code allows to send audio file to selected app.
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_SEND);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(Intent.createChooser(intent, "Choose app"));
More info

View folder contents in Android using default explorer

I want to programatically launch a default file explorer to show me the contents of a folder.
I'm using this code, but it crashes:
Uri startDir = Uri.fromFile(new File("/sdcard/DCIM/Camera"));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(startDir);
startActivity(intent);
LogCat shows "No Activity found to handle the Intent"...
What's my best option to do this? I want the user to see the contents of the folder and be able to click the files (e.g. click a video and launch it with default player, click a PDF and open it etc).
Unfortunately there seem to be no standard way to do this, I was searching for the exact same thing before and couldn't find out any solution. There are 2 alternative methods that might work for you:
1) Use a general intent and let the user pick his/her file manager
This is safe and easy but a little far from what we really want
Uri startDir = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/DCIM/Camera"));
Intent intent = new Intent();
intent.setData(startDir);
intent.setType("*/*");
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
Don't use intent.setType("file/*");, it's not a standard MIME type.
2) Use Specific Intents that the famous file managers provide The well known file managers have their own custom intent filters that accept a directory path which allows simple browsing. Some of them are here: OI file manager, ES Explorer
Maybe you could check if the user have these specific file managers installed and then use this solution, else fallback to general intent.
For now these are the only two options you have. I will update this post if I find out any better solution.
Although I could not get the OI file manager to go to a specific directory, the following opens up the OI File Manager directly and goes to the last directory it was in.
Intent importFileIntent = new Intent();
importFileIntent.setType( "file/*" );
// Does nothing.
// Uri startingDir = Uri.fromFile( new File(
// Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera"));
// importFileIntent.setData( startingDirectory );
importFileIntent.setAction( Intent.ACTION_GET_CONTENT );
// Ensure that there's an activity to handle the intent.
if (importFileIntent.resolveActivity(getPackageManager()) == null) return;
startActivityForResult(importFileIntent, REQUEST_FILE_IMPORT);
If anybody has further success, I would love to hear it.

how to open another app in a tabhost / how to open a word doc within a tabhost of android?

i've been trying to create an app that could open multiple activities with the help of the tabhost. one of the support is i would like to open a word document within my app.
i know how to open one with other app, but i hope it could be open within my app rather then having the need to press back button to return to my app.
the code that i use to open word doc :
File file = new File
(Environment.getExternalStorageDirectory(),"/MLT/student.doc");
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/msword");
startActivity(intent);
i tried to add it to my tab using
File file = new File (Environment.getExternalStorageDirectory(),"/MLT/student.doc");
intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/msword");
spec = tabHost.newTabSpec("Info").setIndicator("Info",
res.getDrawable(R.drawable.ic_tab_info)).setContent(intent);
tabHost.addTab(spec);
but i get runtime error, with the main
01-12 13:16:32.945: E/AndroidRuntime(10066): java.lang.SecurityException: Requesting code from com.infraware.polarisoffice (with uid 10053) to be run in process com.app.mlt (with uid 10128) "
You can pass this intent as content of your specified tab.
But you should be careful - there can be devices, that does not necessary apps for opening doc files, or there can be several apps for this. Any way it is better to use Intent.createChooser() method for such operations.
I tried using the MIME application/msword type it didn't work so I used application/vnd.ms-word

How to open a text file in default view on button click in android

I want users to read instruction to use my android application as a text or pdf file when they click the Instruction button. How should I read a text/pdf file on button click in default view in android?
Likely the best way is to launch an intent with an ACTION_VIEW action and provide the file you desire.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.setType("application/pdf");
startActivity(intent);
Note that this is dependent on there being an application on the device registered to handle the PDF mime type. You can also choose to have just text or perhaps rich text instead by changing the type.

Display Applications that can handle a particular filetype

So, I have the file path of a file in an application & I want to give the user the ability to select from all applications on the phone that can handle/open that file type and then open the file in that applicaton. How do I do this? - performs a similar function to a file manager.
First you have to pick the type of Intent you want. This code picks applications that can send the data through SMS/email/etc utilizing ACTION_SEND.
Intent intent = new Intent(Intent.ACTION_SEND);
Next you need to use putExtra to get the file/message to the Intent.
Uri uri = Uri.fromFile(new File(myFile));
intent.putExtra(Intent.EXTRA_STREAM, uri);
Finally you create a chooser.
Intent chooser = Intent.createChooser(intent, "A Custom Message");
startActivity(chooser);

Categories

Resources