admin管理员组

文章数量:1597400

点击qq或者微信对话框里面的文件跳转到自己的app时的文件路径问题

最近研发一个手机app,当点击qq或者微信里面的文件时,需要把自己的app加入到可用的程序列表。并在打开软件里面对点击的文件做响应的操作。一阵摸索之后可以把自己软件加入到可用的程序列表,到时在程序中对文件做操作时,总是出现各种各样的路径问题。现在把遇到的问题列举如下,并附上解决办法。

1.在qq或者微信里面单击文件时,把自己的软件加入可用列表中

在AndroidManifest.xml文件中加入过滤器,添加到需要直接打开的Activity里面

 <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan"><!--这两行主要是控制TextView输入信息时让Activity适应窗口-->
            
            <!--启动Activity的过滤器-->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
            
            <!--从第三方软件打开本软件的过滤器-->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="application/vnd.google-earth.kml+xml" /><!--本例子是打开一个kml文件-->
            </intent-filter>
        </activity>

过滤器里面的action、category和data根据自己开发的需求调整。

2.从qq跳转的路径问题
在需要打开Activity的onCreate方法里面加入以下代码:

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*************从第三方应用里面打开***********/
        Intent intent1 = getIntent();
        String action = intent1.getAction();
        if (intent1.ACTION_VIEW.equals(action)) {
            Uri uri = intent1.getData();
            String path1 = Uri.decode(uri.getEncodedPath());//在微信或者qq里面的路径
                //从qq里面跳转
                Log.e("=====9999999999999999==", "从qq里面跳转");
                Log.e("=====9999999999999999==", path1);
                int qqweizhi = path1.indexOf("/storage/emulated/0");
                String path = path1.substring(qqweizhi);//分割字符串
                filepath = path;
                Log.e("=====9999999999999999==", filepath);
       }        
    }

打印出的Log信息如下:

07-01 18:17:09.807 6213-6213/qcfp E/=====9999999999999999==: 从qq里面跳转
07-01 18:17:09.807 6213-6213/qcfp E/=====9999999999999999==: /external_files/storage/emulated/0/Android/data/com.tencent.mobileqq/Tencent/QQfile_recv/08区新增构架面---20191112.kml
07-01 18:17:09.807 6213-6213/qcfp E/=====9999999999999999==: /storage/emulated/0/Android/data/com.tencent.mobileqq/Tencent/QQfile_recv/08区新增构架面---20191112.kml

从控制台可以看到原本是个扩展路径,所以需要把路径进行加工,然后得到正确的文件路径

3.从微信跳转的路径问题
一番摸索之后发现从微信里面打开路径和qq里面的又有所不同,所以综合考虑得到最终的解决办法:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*************从第三方应用里面打开***********/
        Intent intent1 = getIntent();
        String action = intent1.getAction();
        if (intent1.ACTION_VIEW.equals(action)) {
            Uri uri = intent1.getData();//这里不要用intent1.getStringData()方法,因为如果路径里面存在中文会出现乱码,所以要用decode()方法处理一下
            String path1 = Uri.decode(uri.getEncodedPath());//在微信或者qq里面的路径,

            int weizhi= path1.indexOf("/external/tencent");
            if(weizhi==-1){
                //从qq里面跳转
                Log.e("=====9999999999999999==", "从qq里面跳转");
                Log.e("=====9999999999999999==", path1);
                int qqweizhi = path1.indexOf("/storage/emulated/0");
                String path = path1.substring(qqweizhi);//分割字符串
                filepath = path;
                Log.e("=====9999999999999999==", filepath);
            }else{
                Log.e("=====9999999999999999==", "从微信里面跳转");
                Log.e("=====9999999999999999==", path1);

                filepath = path1.replace("external/tencent","storage/emulated/0/Tencent");//把微信里面的小写改为大写
                Log.e("=====9999999999999999==", filepath);
            }      
        }      
    }

最终解决了从两个软件跳转的路径问题

本文标签: 自己的文件对话框跳转到路径