如何打开/显示文件(.PDF,.doc)格式,无需外部应用程序? [英] How to open/display documents(.pdf, .doc) without external app?
问题描述
我要创建一个程序,没有外部的应用程序,打开的文档。我需要这个,因为我想用滚动的手机定位(俯仰和横滚)的文件。我创建了屏幕的底部有一个按钮,当我按下按钮,我可以滚动文档了。如果我松开按钮,我不能把它的滚动。所以,如果我打开与外部应用程序的文档,我的按钮消失了,工作的SensorManager没有。
有一个人任何想法来解决这个问题。或请人任何想法,如何滚动文件,在外部应用程序打开的,我的手机定位?
(对不起,我英文)
这是我的清单:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.orientationscrolling
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =17/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.orientationscrolling.MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
< /舱单>
这是我的布局:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的WebView
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / webView1
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:重力=底
机器人:方向=垂直>
<按钮
机器人:ID =@ + ID / mybutt
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =25sp
机器人:文本=卷轴!
机器人:layout_gravity =右/>
< / LinearLayout中>
这是我的code:
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
按钮=(按钮)findViewById(R.id.mybutt);
字符串PDF =http://www.pc-hardware.hu/PDF/konfig.pdf;
字符串DOC =< IFRAME SRC =HTTP://docs.google.com/gview嵌入式=真放; URL = HTTP://www.pc-hardware.hu/PDF/konfig.pdfWIDTH =100 %高度=100%的风格=边界:无;>< / IFRAME>中;
web视图=(web视图)findViewById(R.id.webView1);
webView.getSettings()setJavaScriptEnabled(真)。
webView.getSettings()setPluginsEnabled(真)。
。webView.getSettings()setAllowFileAccess(真正的);
webView.loadData(文件,text / html的,UTF-8);
}
但有一种方式来显示PDF文件使用了调用其他应用程序
这是一种用于显示PDF的Android应用程序被嵌入PDF文档机器人的WebView 使用从支持 http://docs.google.com/viewer
伪
字符串DOC =< IFRAME SRC =HTTP://docs.google.com/viewer URL = +位置,以您的PDF文件+'
WIDTH =100%高度=100%
风格=边界:无;>< / IFRAME>中;
一个样本显示如下
字符串DOC =< IFRAME src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/$p$psentations-tips.ppt&embedded=true'
WIDTH =100%高度=100%
风格=边界:无;>< / IFRAME>中;
code
的WebView WV =(web视图)findViewById(R.id.webView);
wv.getSettings()setJavaScriptEnabled(真)。
wv.getSettings()setPluginsEnabled(真)。
。wv.getSettings()setAllowFileAccess(真正的);
wv.loadUrl(DOC);
//wv.loadData(文件,text / html的,UTF-8);
和清单中提供
<使用-权限的Android:名称=android.permission.INTERNET对/>
请参阅 这
注意:我不知道与不同的Android版本的兼容性问题
在这种方法的缺点是,你需要互联网连接。但我认为它满足您的需求。
编辑 试试这个作为SRC支持iframe的
<$p$p><$c$c>src="http://docs.google.com/gview?embedded=true&url=http://www.pc-hardware.hu/PDF/konfig.pdf"
尝试 wv.loadData(文件,text / html的,UTF-8);
。这两个对我的作品
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?
(Sorry for my English)
This is my manifest:
<?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>
This is my Layout:
<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>
This is my Code:
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
But there is a way for displaying PDF with out calling another application
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
pseudo
String doc="<iframe src='http://docs.google.com/viewer?url=+location to your PDF File+'
width='100%' height='100%'
style='border: none;'></iframe>";
a sample is is shown below
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>";
Code
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");
and in manifest provide
<uses-permission android:name="android.permission.INTERNET"/>
See this
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
EDIT Try this as src for iframe
src="http://docs.google.com/gview?embedded=true&url=http://www.pc-hardware.hu/PDF/konfig.pdf"
try wv.loadData( doc , "text/html", "UTF-8");
. Both works for me
这篇关于如何打开/显示文件(.PDF,.doc)格式,无需外部应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!