android 打开音视频图片文件(支持所有格式格式)

本文介绍了如何使用Android的FileUtils工具类来实现对音视频和图片各种格式文件的打开支持,包括详细的操作步骤和可能遇到的问题解决方法。
摘要由CSDN通过智能技术生成


打开方式 

AdapterView.OnItemLongClickListener itemchivklong = new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        FileUtils.openFiles(mContext, mFilePath.get(position));
        return false;
    }
};

FileUtils:


public static void openFiles(Context context,String filesPath) {
    Uri uri = Uri.parse("file://" + filesPath);
    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Intent.ACTION_VIEW);

    String type = getMIMEType(filesPath);
    intent.setDataAndType(uri, type);
    if (!type.equals("*/*")) {
        try {
            context.  startActivity(intent);
        } catch (Exception e) {
            context.  startActivity(showOpenTypeDialog(filesPath));
        }
    } else {
        context.    startActivity(showOpenTypeDialog(filesPath));
    }
}
public class FileUtils{
 

public void show(Context context, String filesPath) {
    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Intent.ACTION_VIEW);
    context.startActivity(showOpenTypeDialog(filesPath));
}

public static Intent showOpenTypeDialog(String param) {
   
    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(android.content.Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(new File(param));
    intent.setDataAndType(uri, "*/*");
    return intent;
}

/**
 * --获取文件mimetype --
 */
public static String getMIMEType(String filePath) {
    String type = "*/*";
    String fName = filePath;

    int dotIndex = fName.lastIndexOf(".");
    if (dotIndex < 0) {
        return type;
    }

    String end = fName.substring(dotIndex, fName.length()).toLowerCase();
    if (end == "") {
        return type;
    }

    for (int i = 0; i < MIME_MapTable.length; i++) {
        if (end.equals(MIME_MapTable[i][0])) {
            type = MIME_MapTable[i][1];
        }
    }
    return type;
}

/**
 * -- MIME 列表 --
 */
public static final String[][] MIME_MapTable =
        {
                // --{后缀名, MIME类型}   --
                {
 ".3gp", "video/3gpp"},
                {
 ".3gpp", "video/3gpp"},
                {
 ".aac", "audio/x-mpeg"},
                {
 ".amr", "audio/x-mpeg"},
                {
 ".apk", "application/vnd.android.package-archive"},
                {
 ".avi", "video/x-msvideo"},
                {
 ".aab", "application/x-authoware-bin"},
                {
 ".aam", "application/x-authoware-map"},
                {
 ".aas", "application/x-authoware-seg"},
                {
 ".ai", "application/postscript"},
                {
 ".aif", "audio/x-aiff"},
                {
 ".aifc", "audio/x-aiff"},
                {
 ".aiff", "audio/x-aiff"},
                {
 ".als", "audio/x-alpha5"},
                {
 ".amc", "application/x-mpeg"},
                {
 ".ani", "application/octet-stream"},
                {
 ".asc", "text/plain"},
                {
 ".asd", "application/astound"},
                {
 ".asf", "video/x-ms-asf"},
                {
 ".asn", "application/astound"},
                {
 ".asp", "application/x-asap"},
                {
 ".asx", " video/x-ms-asf"},
                {
 ".au", "audio/basic"},
                {
 ".avb", "application/octet-stream"},
                {
 ".awb", "audio/amr-wb"},
                {
 ".bcpio", "application/x-bcpio"},
                {
 ".bld", "application/bld"},
                {
 ".bld2", "application/bld2"},
                {
 ".bpk", "application/octet-stream"},
                {
 ".bz2", "application/x-bzip2"},
                {
 ".bin", "application/octet-stream"},
                {
 ".bmp", "image/bmp"},
                {
 ".c", "text/plain"},
                {
 ".class", "application/octet-stream"},
                {
 ".conf", "text/plain"},
                {
 ".cpp", "text/plain"},
                {
 ".cal", "image/x-cals"},
                {
 ".ccn", "application/x-cnc"},
                {
 ".cco", "application/x-cocoa"},
                {
 ".cdf", "application/x-netcdf"},
                {
 ".cgi", "magnus-internal/cgi"},
                {
 ".chat", "application/x-chat"},
                {
 ".clp", "application/x-msclip"},
                {
 ".cmx", "application/x-cmx"},
                {
 ".co", "application/x-cult3d-object"},
                {
 ".cod", "image/cis-cod"},
                {
 ".cpio", "application/x-cpio"},
                {
 ".cpt", "application/mac-compactpro"},
                {
 ".crd", "application/x-mscardfile"},
                {
 ".csh", "application/x-csh"},
                {
 ".csm", "chemical/x-csml"},
                {
 ".csml", "chemical/x-csml"},
                {
 ".css", "text/css"},
                {
 ".cur", "application/octet-stream"},
                {
 ".doc", "application/msword"},
                {
 ".dcm", "x-lml/x-evm"},
                {
 ".dcr", "application/x-director"},
                {
 ".dcx", "image/x-dcx"},
                {
 ".dhtml", "text/html"},
                {
 ".dir", "application/x-director"},
                {
 ".dll", "application/octet-stream"},
                {
 ".dmg", "application/octet-stream"},
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值