Allow user to chose application for open file - android

In application user can download file and open it after download completed.
I can open window for choosing application something like this:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + path);
intent.setDataAndType(uri,"image/*");
context.startActivity(intent);
But here "image/*" is hard-coded. In my application user can download files with different extensions (txt, gif, png, doc, .etc).
Of course I can do something like this:
if(fileExt.equels("png") || fileExt.equels("jpg") || fileExt.equels("gif") )
intent.setDataAndType(uri,"image/*");
But then i need to write code for all extensions =/
So, can you suggest better way to filter this file extensions?

You can use activity chooser: (of course you need to provide MIME type as image/* so that only applications which can handle images will appear in the activity chooser.
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}

Related

Android open/view pdf: intent chooser cannot find any application for opening pdf file stored in 'Google Drive'

I was trying to create a chooser to grant users freedom to open a pdf file with the application they prefer.
Here's my approach:
private void openPdf(String pdfUrl, String mime) {
// Intent pdfViewIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
Intent pdfViewIntent = new Intent(Intent.ACTION_VIEW);
pdfViewIntent.setDataAndType(Uri.parse(pdfUrl), mime);
Log.d(TAG, "openPdf: uri: " + Uri.parse((pdfUrl)));
Intent chooser = Intent.createChooser(pdfViewIntent, "Choose an app");
// chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // optional
startActivity(chooser);
finish();
}
The pdf url is something like this: https://drive.google.com/file/d/.../view?usp=sharing
And for the mime type, I tried using both "application/vnd.google-apps.document" and "application/pdf".
Now, when the user clicks on document, I get an output like the following:
But I want an output like the following screenshot: (it appears when I try to open the same link with WhatsApp)
NOTE 1: using WebView is not a good option for me.
NOTE 2: the above approach works if the URL is in the format https://example.com/.../filename.pdf. But it fails for Google Drive's link.

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

I need to open my .doc file in quickoffice in my app without looking for suitable apps (options)

I need to open my .doc file in quickoffice in my app without looking for suitable apps (options).
Here I have used this
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
//String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
// MimeTypeMap.getFileExtensionFromUrl(uri.toString()));
//intent.setDataAndType(uri, type == null ? "*/*" : type);
intent.setDataAndType(uri, "application/msword");
startActivity((Intent.createChooser(intent,
getString(R.string.open_using))));
This Code shows suitable target apps which i dont need actually
Remove the createChooser call, and just pass the intent to startActivity. Then it will open the default app for the mime type, assuming one is set. If none is set, it may still pop up a chooser among those apps which claim they can open it. If you want to only open quick office you can do so by activity name, but then it will fail (and possibly throw an exception) if quick office is not installed.
It worked for me.
PackageManager packageManager = getPackageManager();
Intent quickOffice = packageManager
.getLaunchIntentForPackage("com.quickoffice.android");
File sdcard = Environment.getExternalStorageDirectory();
//your file location
File file = new File(sdcard, "Meta Data.doc");
Uri path = Uri.fromFile(file);
quickOffice.setAction(Intent.ACTION_VIEW);
quickOffice.setDataAndType(path, "application/msword");// ---->application/msword
startActivity(quickOffice);

How to open the My Files folder in Android Programatically (using Intent)?

I am using the below code which opens up the Gallery, Music Player, Dropbox and Contacts, i want the My Files folder to get open programatically, please let me know if there are any specific intent parameters i need to pass to get the File Manager open.
if it is not possible through intent then please give me a snippet or an hint to open the My Files folder programatically.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "View Default File Manager");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
Thanks.
You can use this code to file the files.
int PICKFILE_RESULT_CODE=1;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
this will help you to browse the files from your storage.
Its best that you include a library in your project which handles this scenario.
This worked for me:
This library shows the list of third-party apps. It also has its own file browser for selecting files.
Bad thing is, most Android distributions may or may not ship with a file manager, and even so, may be not with the one which handles CHOOSE_FILE_REQUESTCODE.
So, you are left to create your own file picker activity. Luckily there are many ready made ones available:
http://code.google.com/p/android-filechooser/
https://developers.inkfilepicker.com/docs/android/
If you want to open samsung My Files application try this below code.
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
startActivityForResult(intent, CHOOSE_FILE_REQUESTCODE);
You have to specifically mention the package name of the explorer application. Please find the example below to open a specific folder in ES Explorer.
public void openfolderInexplorer(String path){
Intent intent = this.getPackageManager().getLaunchIntentForPackage("com.estrongs.android.pop");
if (intent != null) {
// If the application is avilable
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.parse(path);
intent.setDataAndType(uri, "resource/folder");
this.startActivity(intent);
} else {
// Play store to install app
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" +
"com.estrongs.android.pop"));
this.startActivity(intent);
}
try this below code. if any file manager available , then it will pop up in a form of menu to choose appropriate for the user.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, CHOOSE_FILE_REQUESTCODE);

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