【问题标题】:Choosing Intent for Zip files为 Zip 文件选择 Intent
【发布时间】:2016-06-30 15:37:47
【问题描述】:

我有一个从 url 下载的 zip 文件,它的路径设置为 /storage/emulated/0/myapp/downloadedfile.zip

现在我想通过选择意图打开这个 zip 文件,列出可用的应用程序来打开下载的文件。

我已在清单中设置:

 <activity
      android:name=".MyApp"
      android:alwaysRetainTaskState="true"
      android:launchMode="singleInstance"
      android:theme="@style/MyMaterialTheme">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="application/zip"/>
            </intent-filter>
</activity>

现在我调用这种方式来打开意图选择器

File downloadedfile = new File(Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + "/myapp") + "/" + "downloadedfile.zip");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(downloadedfile), "application/zip");
startActivity(intent);

如果我有 ESFileExplorer 已经安装在我的应用程序上,这很好用

但我想检查是否有任何预安装的应用程序可用,否则我需要显示 playstore 作为选项之一,以便用户可以下载应用程序并安装ESFileExplorer 应用程序。

那我该怎么做呢?

【问题讨论】:

    标签: android android-intent zip android-intent-chooser


    【解决方案1】:

    Android Intent Zip 文件

    try{
        File downloadedfile = new File(Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + "/myapp") + "/" + "downloadedfile.zip");
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(downloadedfile), "application/zip");
        startActivity(intent);
    }catch (ActivityNotFoundException Ae){
        //When No application can perform zip file
        Uri uri = Uri.parse("market://search?q=" + "application/zip");
        Intent myAppLinkToMarket = new Intent(Intent.ACTION_VIEW, uri);
    
        try {
             startActivity(myAppLinkToMarket);
        } catch (ActivityNotFoundException e) {
             //the device hasn't installed Google Play
             Toast.makeText(MainActivity.this, "You don't have Google Play installed", Toast.LENGTH_LONG).show();
        }
    }
    

    【讨论】:

    • @SID- 这会打开发送意图
    • 检查更新的答案
    • @coder 首先搜索 zip 意图。如果没有找到,则为支持 zip 文件的应用程序打开 playstore。
    【解决方案2】:

    您可以使用 ActivityNotFoundException 捕获 startActivity 意图,然后您可以在 Play 商店中启动 ES File Explorer 市场应用程序。

    【讨论】:

    • 有没有cmets或者你要我给你写代码吗?
    猜你喜欢
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 2015-12-27
    • 2021-11-08
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多