如何在没有外部应用程序的情况下打开/显示文档(.pdf、.doc)? [英] How to open/display documents(.pdf, .doc) without external app?

查看:27
本文介绍了如何在没有外部应用程序的情况下打开/显示文档(.pdf、.doc)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个程序,无需外部应用即可打开文档.我需要这个,因为我想用手机方向(俯仰和滚动)滚动文档.我在屏幕底部创建了一个按钮,当我按住该按钮时,我也可以滚动文档.如果我松开按钮,则无法滚动它.因此,如果我使用外部应用程序打开文档,我的按钮就会消失,并且 sensorManager 也不工作.

I want to create a program, that open documents without external app. I need this, because i want to scroll the document with the phones orientation(Pitch and Roll). I create a button on the bottom of the screen, and when i hold down the button, i can scroll the document too. If i release the button, i can't scroll it. So, if i open the document with external app, my button disappears, and the sensorManager works neither.

有什么想法可以解决这个问题.或者有人知道如何滚动文档,在外部应用程序中打开,我的手机方向?

Have someone any idea to solve this problem. Or have someone any idea, how to scroll the document, opened in an external app, with my phones orientation?

(对不起我的英语)

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orientationscrolling"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.orientationscrolling.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<WebView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical" >

<Button 
   android:id="@+id/mybutt"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="25sp"
   android:text="Scroll!!"
   android:layout_gravity="right"/>
</LinearLayout>

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    button = (Button) findViewById( R.id.mybutt );

    String pdf = "http://www.pc-hardware.hu/PDF/konfig.pdf";
    String doc="<iframe src='http://docs.google.com/gview?embedded=true&url=http://www.pc-hardware.hu/PDF/konfig.pdf' width='100%' height='100%' style='border: none;'></iframe>";
    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginsEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.loadData( doc , "text/html", "UTF-8");
}

推荐答案

我认为你应该使用自定义库来完成.参见 这个这个

I think you should use custom library for getting that done .See this and this

但是有一种方法可以在不调用其他应用程序的情况下显示 PDF

But there is a way for displaying PDF with out calling another application

这是一种在 Android 应用程序中显示 PDF 的方法,该应用程序使用 http://docs.google.com/viewer 的支持将 PDF 文档嵌入到 android webview

This is a way for showing PDF in android app that is embedding the PDF document to android webview using support from http://docs.google.com/viewer

String doc="<iframe src='http://docs.google.com/viewer?url=+location to your PDF File+' 
              width='100%' height='100%' 
              style='border: none;'></iframe>";

示例如下

 String doc="<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true' 
              width='100%' height='100%' 
              style='border: none;'></iframe>";

代码

    WebView  wv = (WebView)findViewById(R.id.webView); 
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setPluginsEnabled(true);
    wv.getSettings().setAllowFileAccess(true);
    wv.loadUrl(doc);
    //wv.loadData( doc, "text/html",  "UTF-8");

并在清单中提供

<uses-permission android:name="android.permission.INTERNET"/>

参见这个

注意:我不知道与各种安卓版本的兼容性问题

Caution : I am not aware of compatibility issues with various android versions

这种方法的缺点是您需要互联网连接.但我认为它满足您的需求

In this approach the drawback is you need internet connectivity . But i think it satisfy your need

编辑试试这个作为 iframe 的 src

EDIT Try this as src for iframe

src="http://docs.google.com/gview?embedded=true&url=http://www.pc-hardware.hu/PDF/konfig.pdf"

尝试 wv.loadData( doc , "text/html", "UTF-8"); .两者都适合我

try wv.loadData( doc , "text/html", "UTF-8"); . Both works for me

这篇关于如何在没有外部应用程序的情况下打开/显示文档(.pdf、.doc)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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