Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACTION_VIEW intent for a file with unknown MimeType

Tags:

android

My app has a feature that browse files on your phone and SD card and open them using other apps. I want a solution where I don't have to specify the MimeType and can work with any type of file.

Here is my code:

Intent myIntent = new Intent(Intent.ACTION_VIEW); myIntent.setData(Uri.fromFile(item)); startActivity(myIntent); 

However, I'm getting the following error:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.PICK dat=file:///sdcard/dropbox/test.pdf } 
like image 778
dannyroa Avatar asked Jun 07 '11 12:06

dannyroa


People also ask

What is the use of Intent createChooser () method?

Most commonly, ACTION_SEND action sends URL of build-in Browser app. While sharing the data, Intent call createChooser() method which takes Intent object and specify the title of the chooser dialog. Intent. createChooser() method allows to display the chooser.

What will happen if there is any action in an implicit Intent?

An implicit intent specifies an action that can invoke any app on the device able to perform the action. Using an implicit intent is useful when your app cannot perform the action, but other apps probably can and you'd like the user to pick which app to use.

What is the purpose of Intent filter in android?

An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.


2 Answers

This should detect the mimetype and open with default:

MimeTypeMap myMime = MimeTypeMap.getSingleton(); Intent newIntent = new Intent(Intent.ACTION_VIEW); String mimeType = myMime.getMimeTypeFromExtension(fileExt(getFile()).substring(1)); newIntent.setDataAndType(Uri.fromFile(getFile()),mimeType); newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try {     context.startActivity(newIntent); } catch (ActivityNotFoundException e) {     Toast.makeText(context, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } 

Using this function:

private String fileExt(String url) {     if (url.indexOf("?") > -1) {         url = url.substring(0, url.indexOf("?"));     }     if (url.lastIndexOf(".") == -1) {         return null;     } else {         String ext = url.substring(url.lastIndexOf(".") + 1);         if (ext.indexOf("%") > -1) {             ext = ext.substring(0, ext.indexOf("%"));         }         if (ext.indexOf("/") > -1) {             ext = ext.substring(0, ext.indexOf("/"));         }         return ext.toLowerCase();      } } 
like image 69
NoBugs Avatar answered Oct 06 '22 02:10

NoBugs


createChooser should do the trick:

Intent myIntent = new Intent(Intent.ACTION_VIEW); myIntent.setData(Uri.fromFile(item)); Intent j = Intent.createChooser(myIntent, "Choose an application to open with:"); startActivity(j); 
like image 35
BadSkillz Avatar answered Oct 06 '22 00:10

BadSkillz