【问题标题】:Android how to open a .doc extension file?Android如何打开.doc扩展文件?
【发布时间】:2022-01-24 21:33:41
【问题描述】:

有什么方法可以打开 .doc 扩展文件吗?

【问题讨论】:

  • 打开文件是什么意思。您要查看文件还是要使用文件的内容。
  • 在安卓市场试用 Quick office 以打开 ms word、excel、pdf 文件查看器
  • 老兄,我想在我的应用活动中打开
  • stackoverflow.com/questions/2902689/… 这可能会有所帮助。

标签: android doc


【解决方案1】:

与 iOS 不同,Android 本身不支持渲染 .doc 或 .ppt 文件。您正在寻找一个公共意图,允许您的应用程序重用其他应用程序的活动来显示这些文档类型。但这仅适用于安装了支持此 Intent 的应用的手机。

http://developer.android.com/guide/topics/intents/intents-filters.html

或者如果你已经安装了一些应用,那么使用这个 Intent:

//Uri uri = Uri.parse("file://"+file.getAbsolutePath());
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type = "application/msword";
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);  

【讨论】:

  • 这对我有用,但我必须将 Target API 设置为
  • 我的word文档总是以“只读”方式打开,有什么建议吗?我为这个问题开了一个问题here
  • 我不能在我的活动中打开文档文件吗?像 pdfView 用于 pdf 文件..
【解决方案2】:

这里有一个方法可以帮你解决这个问题:

public void openDocument(String name) {
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    File file = new File(name);
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    if (extension.equalsIgnoreCase("") || mimetype == null) {
        // if there is no extension or there is no definite mimetype, still try to open the file
        intent.setDataAndType(Uri.fromFile(file), "text/*");
    } else {
        intent.setDataAndType(Uri.fromFile(file), mimetype);            
    }
    // custom message for the intent
    startActivity(Intent.createChooser(intent, "Choose an Application:"));
}

【讨论】:

  • if (intent.resolveActivity(getPackageManager()) != null) { startActivity(Intent.createChooser(intent, "Choose an Application:")); } else { Toast.makeText(ctxt, "没有安装合适的应用程序。", Toast.LENGTH_SHORT).show(); }
【解决方案3】:

从可用应用程序列表中打开文档 用户必须从应用程序列表中选择应用程序

File targetFile = new File(path);
                    Uri targetUri = Uri.fromFile(targetFile);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(targetUri, "application/*");
                    startActivityForResult(intent, DOC);

【讨论】:

  • 那么,startActivityForResult() 中的request_code 是什么。 DOC 不代表特定的请求代码。
【解决方案4】:

如果您想在应用程序中打开文件,您可以在 webview 中打开文件。 例如:

 String doc="<iframe src='http://docs.google.com/viewer?    url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true'"+
    " width='100%' height='100%' style='border: none;'></iframe>";

        WebView  wv = (WebView)findViewById(R.id.fileWebView); 
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setAllowFileAccess(true);
        //wv.loadUrl(doc);
        wv.loadData( doc , "text/html",  "UTF-8");

【讨论】:

  • 我正在尝试查看我必须使用哪些选项来创建文件预览,但不知道您可以向 Google 文档发送 URL!
  • 但文件在本地资产文件夹中。您正在描述一种显示远程文件的方法
  • 它显示宽屏我们可以放入移动设备中,您有任何解决方案
【解决方案5】:

这是在 Android 7.0 或更低版本中打开 .doc 文件的完整方法:

第 1 步: 首先,将您的 pdf 文件放在 assets 文件夹中,如下图所示。

第 2 步: 现在转到 build.gradle 文件并添加以下行:

repositories {
maven {
    url "https://s3.amazonaws.com/repo.commonsware.com"
}

}

然后在依赖项下添加以下行并同步:

compile 'com.commonsware.cwac:provider:0.4.3'

第三步: 现在添加一个新的 java 文件,它应该从 FileProvider 扩展,就像在我的例子中文件名是 LegacyCompatFileProvider 和里面的代码一样。

import android.database.Cursor;
import android.net.Uri;

import android.support.v4.content.FileProvider;

import com.commonsware.cwac.provider.LegacyCompatCursorWrapper;

public class LegacyCompatFileProvider extends FileProvider {
  @Override
  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    return(new LegacyCompatCursorWrapper(super.query(uri, projection, selection, selectionArgs, sortOrder)));
  }
}

第四步:"res" 文件夹下创建一个名为"xml" 的文件夹。 (如果文件夹已经存在,则无需创建)。现在在xml 文件夹中添加一个providers_path.xml 文件。这是屏幕截图:

在文件内添加以下行:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="stuff" />
</paths>

第五步: 现在转到AndroidManifest.xml 文件和&lt;application&gt;&lt;/application&gt; 标签中的以下行:

<provider
            android:name="LegacyCompatFileProvider"
            android:authorities="REPLACE_IT_WITH_PACKAGE_NAME"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

第 6 步: 现在转到要加载 pdf 的 Activity 类并添加以下 1 行和这 2 个方法:

private static final String AUTHORITY="REPLACE_IT_WITH_PACKAGE_NAME";

static private void copy(InputStream in, File dst) throws IOException {
        FileOutputStream out=new FileOutputStream(dst);
        byte[] buf=new byte[1024];
        int len;

        while ((len=in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        in.close();
        out.close();
    }

    private  void LoadPdfFile(String fileName){

        File f = new File(getFilesDir(), fileName + ".doc");

        if (!f.exists()) {
            AssetManager assets=getAssets();

            try {
                copy(assets.open(fileName + ".doc"), f);
            }
            catch (IOException e) {
                Log.e("FileProvider", "Exception copying from assets", e);
            }
        }

        Intent i=
                new Intent(Intent.ACTION_VIEW,
                        FileProvider.getUriForFile(this, AUTHORITY, f));

        i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        startActivity(i);
        finish();
    }

现在调用LoadPdfFile 方法并传递您的文件名而不传递.doc,就像在我的情况下"chapter-0" 一样,它将在文档阅读器应用程序中打开文档文件。

【讨论】:

    【解决方案6】:

    您可以将文件从原始资源复制到 sdcard,然后在 ACTION_VIEW Intent 上调用 startActivity(),该意图具有指向可读副本的 Uri 并且还具有正确的 MIME 类型。

    当然,这仅适用于具有 Word 文档查看器的设备。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多