android 系统自带分享功能之pdf分享并且打开

          公司做的app有一个分享pdf文件的功能,产品经理明说要用系统自带的分享。

首先在AndroidManifest.xml中配置:

<activity
    android:name="com.artifex.mupdfdemo.MedlivePDFActivity"
    android:configChanges="orientation|screenSize|keyboardHidden"
    android:label="@string/sensors_guideline_rank_detail"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:mimeType="application/pdf" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <intent-filter>
</activity>
   第二步就是在activity中写分享并打开的代码了。
  
/**
 * @param pdfUrl要打开的文件的绝对路径
 * 
 */
public Intent getPdfFileIntent(String pdfUrl) {
    Intent intent = new Intent(Intent.ACTION_VIEW );
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Uri uri = Uri.fromFile(new File(pdfUrl));
    //intent.putExtra(Intent.EXTRA_STREAM, uri);
    //intent.setType("application/*");
    intent.setDataAndType(uri, "application/pdf");
    return intent;
}
//点击事件
sharePdfBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String file_name = bundle.getString("file_name");
            String path = DOWNLOAD_FILE_PATH + file_name;
            Intent intent = getPdfFileIntent(path);
            startActivity(intent);
        } else {
            showToast("PDF文件出了些故障");
            return;
        }
    }
});
这样就可以了。


版权声明:本文为weixin_39489115原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。