【问题标题】:How to open/display documents(.pdf, .doc) without external app?如何在没有外部应用程序的情况下打开/显示文档(.pdf、.doc)?
【发布时间】:2013-01-12 18:21:51
【问题描述】:

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

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

(对不起我的英语)

这是我的清单:

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

【问题讨论】:

  • 您找到解决方案了吗?我也面临这个问题。我无法在 webview 中加载文档。
  • 同样的问题,提供的解决方案适用于 pdf 文件,但不适用于 .docs 或 .pptx 文件。
  • @karthi 你的问题解决了吗?

标签: android pdf external


【解决方案1】:

以上依赖于谷歌文档的答案仅在您有互联网连接时才有效。更好的选择是使用 Android PdfRenderer 类。

您可以尝试示例应用here

【讨论】:

    【解决方案2】:

    从这里下载源代码 (Display PDF file inside my android application)

    在您的成绩中添加此依赖项:

    compile 'com.github.barteksc:android-pdf-viewer:2.0.3'
    

    activity_main.xml

    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/colorPrimaryDark"
            android:text="View PDF"
            android:textColor="#ffffff"
            android:id="@+id/tv_header"
            android:textSize="18dp"
            android:gravity="center"></TextView>
    
        <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_below="@+id/tv_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    
        </RelativeLayout>
    

    MainActivity.java

    package pdfviewer.pdfviewer;
    
    import android.app.Activity;
    import android.database.Cursor;
    import android.net.Uri;
    import android.provider.OpenableColumns;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    
    import com.github.barteksc.pdfviewer.PDFView;
    import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
    import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
    import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
    import com.shockwave.pdfium.PdfDocument;
    
    import java.util.List;
    
    public class MainActivity extends Activity implements OnPageChangeListener,OnLoadCompleteListener{
        private static final String TAG = MainActivity.class.getSimpleName();
        public static final String SAMPLE_FILE = "android_tutorial.pdf";
        PDFView pdfView;
        Integer pageNumber = 0;
        String pdfFileName;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            pdfView= (PDFView)findViewById(R.id.pdfView);
            displayFromAsset(SAMPLE_FILE);
        }
    
        private void displayFromAsset(String assetFileName) {
            pdfFileName = assetFileName;
    
            pdfView.fromAsset(SAMPLE_FILE)
                    .defaultPage(pageNumber)
                    .enableSwipe(true)
    
                    .swipeHorizontal(false)
                    .onPageChange(this)
                    .enableAnnotationRendering(true)
                    .onLoad(this)
                    .scrollHandle(new DefaultScrollHandle(this))
                    .load();
        }
    
    
        @Override
        public void onPageChanged(int page, int pageCount) {
            pageNumber = page;
            setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
        }
    
    
        @Override
        public void loadComplete(int nbPages) {
            PdfDocument.Meta meta = pdfView.getDocumentMeta();
            printBookmarksTree(pdfView.getTableOfContents(), "-");
    
        }
    
        public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
            for (PdfDocument.Bookmark b : tree) {
    
                Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));
    
                if (b.hasChildren()) {
                    printBookmarksTree(b.getChildren(), sep + "-");
                }
            }
        }
    
    }
    

    谢谢!

    【讨论】:

    • 此库不提供文件内链接的导航。您还知道任何其他具有内部链接导航的完整证明库吗?
    • 抱歉我不知道
    【解决方案3】:

    好吧,我认为下面是解决方案

     String doc="<iframe src='http://docs.google.com/viewer?url=http://www.example.com/yourfile.pdf&embedded=true' width='100%' height='100%'  style='border: none;'></iframe>";
    
    
        WebView  wv = (WebView)findViewById(R.id.wv);
        wv.setVisibility(WebView.VISIBLE);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setAllowFileAccess(true);
        wv.getSettings().setPluginState(WebSettings.PluginState.ON);  
        wv.setWebViewClient(new Callback()); 
        wv.loadData(doc, "text/html", "UTF-8");   
    

    希望这对你有帮助:)

    【讨论】:

      【解决方案4】:

      我认为您应该使用自定义库来完成这项工作。请参阅 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");
      

      并在清单中提供

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

      this

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

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

      编辑 试试这个作为 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");。两者都适合我

      【讨论】:

      • 令人印象深刻!我永远不会考虑它。
      • 我不确定这是你想要的吗??因为它需要其他服务的支持
      • 由于某种原因它不起作用。这是我手机的显示图片:link 我在清单中添加了权限。这是我的代码:'String doc=""; webView = (WebView) findViewById(R.id.webView1); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setPluginsEnabled(true); webView.getSettings().setAllowFileAccess(true); webView.loadUrl(doc);'
      • 您是否在清单中授予了权限..?你检查的是哪个安卓版本?
      • 我可以用它来显示我 SD 卡中的 pdf 文件吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      相关资源
      最近更新 更多