Jak otwierać / wyświetlać dokumenty(.pdf,doc) bez zewnętrznej aplikacji?

Chcę utworzyć program, który otwiera dokumenty bez zewnętrznej aplikacji. Potrzebuję tego, ponieważ chcę przewijać dokument z orientacją telefonu (Pitch and Roll). Tworzę przycisk na dole ekranu, a kiedy przytrzymam przycisk, mogę również przewijać dokument. Jeśli zwolnię przycisk, nie mogę go przewijać. Tak więc, jeśli otworzę dokument za pomocą zewnętrznej aplikacji, Mój przycisk znika,a sensorManager nie działa.

Mieć jakiś pomysł, aby rozwiązać ten problem. Lub mieć ktoś jakiś pomysł, jak przewijać dokument, otwarty w zewnętrznej aplikacji, z moją orientacją na telefony?

(Sorry for my English)

To jest mój 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>

To jest mój 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>

To jest mój kod:

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");
}
Author: KhalDrogo, 2013-01-29

3 answers

Myślę, że powinieneś użyć niestandardowej biblioteki, aby to zrobić .Zobacz to i to

Ale jest sposób na wyświetlanie plików PDF bez wywoływania innej aplikacji

Jest to sposób na wyświetlanie PDF w aplikacji android, który osadza dokument PDF do android webview przy użyciu wsparcia z 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>";

Próbka jest pokazana poniżej

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

Kod

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

I w oczywisty sposób dostarczyć

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

Zobacz to

UWAGA: nie jestem świadomy problemów ze zgodnością z różnymi wersjami Androida

W tym podejściu wadą jest to, że potrzebujesz połączenia z Internetem . Ale myślę, że zaspokoi Twoje potrzeby

Edytuj Spróbuj tego jako src dla iframe

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

Spróbuj wv.loadData( doc , "text/html", "UTF-8"); . Oba działa dla mnie

Tutaj wpisz opis obrazkaTutaj wpisz opis obrazka

 23
Author: edwin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:17:57

Pobierz kod źródłowy stąd (Wyświetl plik PDF w mojej aplikacji na Androida)

Dodaj tę zależność do swojej oceny:

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>

Główna aktywność.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 + "-");
            }
        }
    }

}
Dzięki!
 1
Author: Deepshikha Puri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-08 08:58:21

Cóż, myślę, że poniżej jest rozwiązanie

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

Mam nadzieję, że to ci pomoże:)

 0
Author: Suresh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-25 13:39:54