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

来源:互联网 发布:云计算架构是什么 编辑:程序博客网 时间:2024/06/01 07:54

          公司做的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;        }    }});
这样就可以了。

阅读全文
0 0
原创粉丝点击