• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

get MIME type

hookmoon

Lurker
Hi everybody,

I tried to run file browser application. I got code from one site . I have - I cannot open files. I think it happens because I have to set right mime type to Intent. BTW this code don't work:

Code:
Intent i = new Intent(android.content.Intent.ACTION_VIEW );
i.setData(Uri.parse("file://" + file.getAbsolutePath()));
startActivity(i);
Can anybody explain me how fix this problem?
 
So I want to open any type of file (so .mp3, .pdf , .txt will be enough). Is it possible close this issue without hardcode or I have to prepare mime in advance?
 
Without actually reading the file into memory I'd say the best was to determine the mime-type is based on extension, so yeah you're probably stuck with a hard-coded lookup, then call myIntent.setType("text/html"); or whatever.


Code:
File root = Environment.getExternalStorageDirectory();
File myFile = new File(root, "whatever.html"); 

Intent i = new  Intent(android.content.Intent.ACTION_VIEW);
i.setType("text/html");   //this is where you will have to hardcode
i.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile( myFile ));
startActivity(Intent.createChooser(i, "Choose program") );
 
Back
Top Bottom