来自TextView的Android pdf链接在Google文档查看器中打开

时间:2015-06-05 15:15:23

标签: java android pdf

我几天都遇到了这个问题。我正在制作可以读取由wordpress网站生成的rss新闻的应用程序。有些帖子有外部pdf文件的链接,我需要在Google文档查看器中从我的应用中打开这些文件。我成功地在TextView和Html(fromHtml ...)中解析新闻,SetMovementh显示可点击的链接并触发浏览器。问题是当链接有.pdf文件时,它们会被下载但是正如预期的那样,默认情况下无法打开。我需要一些方法,可以使用来自TextView的所有链接(href)以.pdf结尾,使用Google文档查看器打开,例如: ...如果url isEnding(.pdf)然后用GogleDocs打开其他常规打开的URL ...

public class ShowDetails extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);

        TextView detailsTitle = (TextView)findViewById(R.id.detailstitle);
        TextView detailsPubdate = (TextView)findViewById(R.id.detailspubdate);
        TextView detailsSadrzajPosta = (TextView)findViewById(R.id.detailssadrzaj_posta);
        TextView detailsLink = (TextView)findViewById(R.id.detailslink);

        Bundle bundle = this.getIntent().getExtras();

        detailsTitle.setText(bundle.getString("keyTitle"));
        detailsPubdate.setText(bundle.getString("keyPubdate"));
        detailsSadrzajPosta.setText(Html.fromHtml(bundle.getString("keySadrzajPosta")));
        detailsSadrzajPosta.setMovementMethod(LinkMovementMethod.getInstance());

    }

}

我的detail.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/detailstitle" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/detailspubdate" />
   <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/detailssadrzaj_posta"
        />

   <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:id="@+id/detailslink" /> 

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

要在google doc viewer中查看pdf(复制自:https://www.b4x.com/android/forum/threads/intent-for-drive-pdf-viewer.53268/):

Private i As Intent

i.Initialize(i.ACTION_VIEW, "file:///" & File.DirDefaultExternal & "/myPDF.pdf")
i.SetComponent("com.google.android.apps.docs/com.google.android.apps.viewer.PdfViewerActivity")

StartActivity(i)

要覆盖TextView的链接行为,您需要使用Spannable并将TextView中的现有链接(URLSpan)替换为您自己的ClickableSpan。或者更简单的使用库Textoo

结合这两者,解决方案看起来像:

TextView textViewWithLinks = Textoo
    .config((TextView) findViewById(R.id.my_text_view))
    .addLinksHandler(new LinksHandler() {
        @Override
        public boolean onClick(View view, String url) {
            if (url.endsWith(".pdf")) {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                i.setClassName("com.google.android.apps.docs", "com.google.android.apps.viewer.PdfViewerActivity");
                startActivity(i);
                return true;
            } else {
                return false;
            }
        }
    })
    .apply();