Android 仿照第三方app 将自己APP显示在打开列表中

有时候我们需要将文件用我们的APP 打开,就像这样:
这里写图片描述

一,我们需要在清单文件中进行一些信息配置,让它们可以检测到我们的APP。

因为我们需要的文件类型就下面这些,所以加入指定类型,如果所有类型都支持,可以
<data android:mimeType="*/*" />
<intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <!--<data android:mimeType="*/*"/>-->
            <data android:mimeType="video/*" />
            <data android:mimeType="image/*" />
            <data android:mimeType="application/pdf" />
            <data android:mimeType="application/msword" />
            <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document"/>
            <data android:mimeType="application/vnd.ms-excel"/>
            <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
            <data android:mimeType="application/vnd.ms-powerpoint"/>
            <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation"/>

        </intent-filter>

二,接收数据

在起始的activity中做如下处理

if (Intent.ACTION_VIEW.equals(action)) {
            String url = itnIn.getDataString();
            String path = getRealPathFromURI(activity, Uri.parse(url));
            Log.e(TAG, "path:" + path);
            return path;
        }

/**
     * 通过Uri获取文件在本地存储的真实路径
     * @param act
     * @param contentUri
     * @return
     */
    private static String getRealPathFromURI(Activity act, Uri contentUri) {

        // can post image
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = act.managedQuery(contentUri, proj, // Which columns to return
                null, // WHERE clause; which rows to return (all rows)
                null, // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        if (cursor==null) {
            String path = contentUri.getPath();
            return path;
        }

        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);

    }

返回的path就是我们需要的文件路径,有了这个路径我们 就可以为所欲为了。。

下面附上一些常见文件的打开方式:

{".3gp", "video/3gpp"},
{".apk", "application/vnd.android.package-archive"},
{".asf", "video/x-ms-asf"},
{".avi", "video/x-msvideo"},
{".bin", "application/octet-stream"},
{".bmp", "image/bmp"},
{".c", "text/plain"},
{".class", "application/octet-stream"},
{".conf", "text/plain"},
{".cpp", "text/plain"},
{".doc", "application/msword"},
{".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".xls", "application/vnd.ms-excel"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".exe", "application/octet-stream"},
{".gif", "image/gif"},
{".gtar", "application/x-gtar"},
{".gz", "application/x-gzip"},
{".h", "text/plain"},
{".htm", "text/html"},
{".html", "text/html"},
{".jar", "application/java-archive"},
{".java", "text/plain"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "application/x-javascript"},
{".log", "text/plain"},
{".m3u", "audio/x-mpegurl"},
{".m4a", "audio/mp4a-latm"},
{".m4b", "audio/mp4a-latm"},
{".m4p", "audio/mp4a-latm"},
{".m4u", "video/vnd.mpegurl"},
{".m4v", "video/x-m4v"},
{".mov", "video/quicktime"},
{".mp2", "audio/x-mpeg"},
{".mp3", "audio/x-mpeg"},
{".mp4", "video/mp4"},
{".mpc", "application/vnd.mpohun.certificate"},
{".mpe", "video/mpeg"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".mpg4", "video/mp4"},
{".mpga", "audio/mpeg"},
{".msg", "application/vnd.ms-outlook"},
{".ogg", "audio/ogg"},
{".pdf", "application/pdf"},
{".png", "image/png"},
{".pps", "application/vnd.ms-powerpoint"},
{".ppt", "application/vnd.ms-powerpoint"},
{".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".prop", "text/plain"},
{".rc", "text/plain"},
{".rmvb", "audio/x-pn-realaudio"},
{".rtf", "application/rtf"},
{".sh", "text/plain"},
{".tar", "application/x-tar"},
{".tgz", "application/x-compressed"},
{".txt", "text/plain"},
{".wav", "audio/x-wav"},
{".wma", "audio/x-ms-wma"},
{".wmv", "audio/x-ms-wmv"},
{".wps", "application/vnd.ms-works"},
{".xml", "text/plain"},
{".z", "application/x-compress"},
{".zip", "application/x-zip-compressed"},
{"", "*/*"} 

猜你喜欢

转载自blog.csdn.net/qq_34983989/article/details/78560968
今日推荐