【问题标题】:Load .ppt file using Google docs viewer Android使用 Google 文档查看器 Android 加载 .ppt 文件
【发布时间】:2018-06-27 18:45:57
【问题描述】:

我正在尝试在我的 Android 应用程序上读取 ppt 文件。但每次我运行该应用程序时,它都会在 startActivity() 处崩溃。

package com.example.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.ShareCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Uri pptUri = Uri.parse("/home/waheed/check.ppt");
    Intent shareIntent = ShareCompat.IntentBuilder.from(this)
            .setText("open ppt")
            .setType("application/vnd.ms-powerpoint")
            .setStream(pptUri )
            .getIntent()
            .setPackage("com.google.android.apps.docs");
    startActivity(shareIntent);
}
}

【问题讨论】:

    标签: java android google-document-viewer


    【解决方案1】:

    我是这样实现的:

    Uri fileURI = FileProvider.getUriForFile(context, "com.test.myapp", fileFromServer);
            Intent intent = new Intent(Intent.ACTION_VIEW); 
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(fileURI, mimetype);
            intent.setPackage(packageName);
    

    对于包名,我有这些可能的值,具体取决于文件的扩展名:

    • com.dwgsee.dwgviewer
    • cn.wps.moffice_eng​​li>

    我必须提到,我的 Android 上某些版本的某些应用程序存在一些问题,我使用 4.2.2 和 4.4.4 的 Android 版本(我的平板电脑有这些版本),DWG 的某些应用程序在 4.2 上打开没有问题.2 但不在 4.4.4 上

    【讨论】:

    • 我应该写什么来代替“filefromServer”和mimetype?
    • 在我的例子中,fileFromServer 是一个 File 对象,它有一个我从网络上的共享文件夹中获取的文件。在您的情况下,它应该是一个 File 对象,指向您要打开的任何文件。 mimeType 参数只是您要打开的字段的类型,在 google 上搜索它,在我的情况下,我使用 application/acad 用于 DWG 文件或 application/pdf 用于 PDF 文件。
    • 我的应用程序在以下行崩溃....... Uri fileURI = FileProvider.getUriForFile(context, "com.example.myapplication", newFile); ..................我在上面这行之前添加了这两行........ File imagePath = 新文件(context.getFilesDir(),“/home/waheed”); File newFile = new File(imagePath, "check.ppt");
    • 调试时Logcat会说什么?可能文件不存在。您是否将这些行添加到清单中?
    • 它在 log cat 中的同一行再次崩溃............ 由:java.lang。 NullPointerException:尝试在空对象引用上调用虚拟方法 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)'
    猜你喜欢
    • 2012-08-19
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 2013-06-25
    • 2013-01-16
    • 2011-06-22
    相关资源
    最近更新 更多