ACTION_VIEW意图不明MimeType的文件 [英] ACTION_VIEW intent for a file with unknown MimeType

查看:198
本文介绍了ACTION_VIEW意图不明MimeType的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个浏览您的手机和SD卡上的文件,并使用其他应用程序打开它们的功能。我想在那里我没有指定的MimeType,并且可以与任何类型的文件的工作溶液。

下面是我的code:

 意图myIntent =新的意图(Intent.ACTION_VIEW);
myIntent.setData(Uri.fromFile(项目));
startActivity(myIntent);
 

不过,我收到以下错误:

  android.content.ActivityNotFoundException:无活动处理意向{行为= android.intent.action.PICK DAT =文件:///sdcard/dropbox/test.pdf}
 

解决方案

这应检测MIMETYPE并打开默认的:

  MimeTypeMap myMime = MimeTypeMap.getSingleton();
意图newIntent =新的意图(Intent.ACTION_VIEW);
字符串MIMETYPE = myMime.getMimeTypeFromExtension(fileExt(的GetFile())子(1));
newIntent.setDataAndType(Uri.fromFile(的GetFile()),MIMETYPE);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
尝试 {
    context.startActivity(newIntent);
}赶上(ActivityNotFoundException E){
    Toast.makeText(上下文中,无处理程序这种类型的文件,Toast.LENGTH_LONG).show();
}
 

使用此功能:

 私人字符串fileExt(字符串URL){
    如果(?url.indexOf()&-1)〜{
        网址= url.substring(0,url.indexOf()?);
    }
    如果(url.lastIndexOf(。)==  -  1){
        返回null;
    } 其他 {
        串分机= url.substring(url.lastIndexOf()。);
        如果(ext.indexOf(%)&-1)〜{
            分机= ext.substring(0,ext.indexOf(%));
        }
        如果(ext.indexOf(/)-1)〜{
            分机= ext.substring(0,ext.indexOf(/));
        }
        返回ext.toLowerCase();

    }
}
 

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 }

解决方案

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("."));
        if (ext.indexOf("%") > -1) {
            ext = ext.substring(0, ext.indexOf("%"));
        }
        if (ext.indexOf("/") > -1) {
            ext = ext.substring(0, ext.indexOf("/"));
        }
        return ext.toLowerCase();

    }
}

这篇关于ACTION_VIEW意图不明MimeType的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆