【问题标题】:I need to open my .doc file in quickoffice in my app without looking for suitable apps (options)我需要在我的应用程序的 quickoffice 中打开我的 .doc 文件,而无需寻找合适的应用程序(选项)
【发布时间】:2014-06-14 07:38:08
【问题描述】:

我需要在我的应用程序中的 quickoffice 中打开我的 .doc 文件,而无需寻找合适的应用程序(选项)。

这里我用过这个

    Intent intent = new Intent();

    intent.setAction(android.content.Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    //String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
    //      MimeTypeMap.getFileExtensionFromUrl(uri.toString()));
    //intent.setDataAndType(uri, type == null ? "*/*" : type);
    intent.setDataAndType(uri, "application/msword");

    startActivity((Intent.createChooser(intent,
            getString(R.string.open_using))));

此代码显示了我实际上不需要的合适目标应用程序

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    删除 createChooser 调用,并将意图传递给 startActivity。然后它将打开 mime 类型的默认应用程序,假设已设置一个。如果没有设置,它仍然可能会在声称可以打开它的应用程序中弹出一个选择器。如果您只想打开快速办公,您可以通过活动名称进行操作,但如果未安装快速办公,它将失败(并可能引发异常)。

    【讨论】:

    • 谢谢@ Gabe 但我不知道如何获取 quickoffice 的活动名称。我怎么知道?
    • 查看stackoverflow.com/questions/5728903/…,其中包含有关如何手动和以编程方式读取 apk 文件的信息。
    【解决方案2】:

    它对我有用。

            PackageManager packageManager = getPackageManager();
            Intent quickOffice = packageManager
                .getLaunchIntentForPackage("com.quickoffice.android");
    
            File sdcard = Environment.getExternalStorageDirectory();
            //your file location
            File file = new File(sdcard, "Meta Data.doc");
            Uri path = Uri.fromFile(file);
    
            quickOffice.setAction(Intent.ACTION_VIEW);
            quickOffice.setDataAndType(path, "application/msword");// ---->application/msword
    
            startActivity(quickOffice);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 2011-08-02
      相关资源
      最近更新 更多