gpt4 book ai didi

android - 如何在没有外部应用程序的情况下打开/显示文档(.pdf、.doc)?

转载 作者:IT老高 更新时间:2023-10-28 23:10:13 27 4
gpt4 key购买 nike

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

有任何想法来解决这个问题。或者有任何想法,如何滚动在外部应用程序中打开的文档,以我的手机方向?

(对不起我的英语)

这是我的 list :

<?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");
}

最佳答案

我认为您应该使用自定义库来完成这项工作。参见 thisthis

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

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

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");

并在 list 中提供

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

this

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

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

编辑试试这个作为 iframe 的 src

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

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

enter image description here enter image description here

关于android - 如何在没有外部应用程序的情况下打开/显示文档(.pdf、.doc)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14578530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com