Android Intent Filer用于打开pdf文件 [英] Android intent filer for opening pdf files

查看:88
本文介绍了Android Intent Filer用于打开pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将pdf查看器应用程序添加到了pdf文件的使用完成操作"列表中.但是,如果我在列表上运行文件,则该应用程序将关闭.

I added my pdf viewer app to "Complete action using" list for pdf files. But if I run a file on the list, the app just shut down.

我在onCreate方法和清单文件中创建了意图过滤器,但我不知道如何解决此错误.

I made intent filter in onCreate method and manifest file, but I couldn't figure out how to fix this error.

PdfActivity.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pdf); //xml file for pdf view page

    Intent intent = getIntent();

    if(intent != null) {
        if(Intent.ACTION_VIEW.equals(intent.getAction())) {
            startActivity(intent);
        }
    }

    init();
}

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".PdfActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="content" />
            <data android:scheme="file" />
            <data android:mimeType="application/pdf" />
        </intent-filter>
    </activity>

</application>

请帮助我解决这个问题.

Please help me in this problem.

推荐答案

尝试一下

  //Method to generate a MIME Type
  private static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}

 //Method to generate File URI
 private static Uri getFileUri(String filePath) {
    Uri fileUri = null;
    File file = new File(filePath);
    try {
        if (Build.VERSION.SDK_INT >= 24) {
            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
            m.invoke(null);
        }
        fileUri = Uri.fromFile(file);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return fileUri;
}


 //Intent
 Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setDataAndType(getFileUri(file_path), getMimeType(file_path));
 startActivity(intent);

这篇关于Android Intent Filer用于打开pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆