0
String filename = Environment.getExternalStorageDirectory()+"/word.docx";
try{
    File file = new File(filename);
    Uri path = Uri.fromFile(file);
    Intent viewDoc = new Intent(Intent.ACTION_VIEW);
        Log.i(TAG, "mime type "+URLConnection.guessContentTypeFromName(filename).toString());
    viewDoc.setDataAndType(path, URLConnection.guessContentTypeFromName(filename));

    PackageManager pm = getPackageManager();
    List<ResolveInfo> apps = 
        pm.queryIntentActivities(viewDoc, PackageManager.MATCH_DEFAULT_ONLY);

    if (apps.size() > 0){
        startActivity(viewDoc);
    }
}catch(Exception e){
    Log.i(TAG, "oh exception "+e.getMessage());
}

That is my code and I'm not getting the mime type in Android 2.1, I get null in that Log.

Though, I'll able to get it in the Android 2.3.3 with the same code same app.

SkyWalker
  • 855
  • 2
  • 14
  • 36

1 Answers1

2

You need to pass a url to the function. Try prefixing it with "file://"

Update: moving comments to answer: you need to use MimeTypeMap instead of the URLConnection's guessContentTypeFromName. See this SO thread for an example

Community
  • 1
  • 1
Attila
  • 28,265
  • 3
  • 46
  • 55
  • I'm passing /sdcard/word.docx . Is it okay? or i pass filename://sdcard/word.docx like this – SkyWalker Apr 17 '12 at 11:20
  • file:///sdcard/word.docx -- observe the three `/`s: two for the file protocol, one for the root access – Attila Apr 17 '12 at 11:25
  • no, that doesn't work. I got Exception. I don't know where is problem it works in android 2.3.3. – SkyWalker Apr 17 '12 at 11:33
  • It go in catch block but the exception is null. – SkyWalker Apr 17 '12 at 11:43
  • sorry, the exception is caused by the guessContentTypeFromName().toString in Log. Now, I didn't get any exception and mime type is null. – SkyWalker Apr 17 '12 at 11:47
  • 1
    I'm not familiar with android (I was hoping I could guess), but this [SO thread](http://stackoverflow.com/questions/8589645/how-to-detemine-mime-type-of-file-in-android) seems to be after the same thing, try that approach – Attila Apr 17 '12 at 11:58
  • Okay, I'll see that and try to get something. Thanks for your help. – SkyWalker Apr 17 '12 at 12:01
  • oh that code works, thanks a lot :) I serched in stackoverflow but didn't get this question. – SkyWalker Apr 17 '12 at 12:12