Je veux créer un programme, qui ouvre des documents sans application externe. J'ai besoin de cela car je veux faire défiler le document avec l'orientation des téléphones (Pitch and Roll). Je crée un bouton en bas de l'écran et, lorsque je le maintient enfoncé, je peux aussi faire défiler le document. Si je relâche le bouton, je ne peux pas le faire défiler. Donc, si j'ouvre le document avec une application externe, mon bouton disparaît et sensorManager ne fonctionne pas non plus.
Avez-vous une idée pour résoudre ce problème. Ou avez-vous une idée, comment faire défiler le document, ouvert dans une application externe, avec l'orientation de mon téléphone?
(Désolé pour mon anglais)
Ceci est mon manifeste:
<?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>
Ceci est ma mise en page:
<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>
Ceci est mon 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");
}
Je pense que vous devriez utiliser une bibliothèque personnalisée pour y arriver. Voir this et this
Mais il existe un moyen d'afficher PDF sans appeler une autre application.
C'est un moyen d'afficher PDF dans Android application incorporant le document PDF à la vue Web Android en utilisant le support de 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>";
un échantillon est montré ci-dessous
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");
et dans manifeste fournir
<uses-permission Android:name="Android.permission.INTERNET"/>
Voir this
Attention: je ne suis pas au courant de problèmes de compatibilité avec diverses versions Android
Dans cette approche, l'inconvénient est que vous avez besoin d'une connexion Internet. Mais je pense que cela répond à vos besoins
EDIT Essayez ceci comme src pour iframe
src="http://docs.google.com/gview?embedded=true&url=http://www.pc-hardware.hu/PDF/konfig.pdf"
essayez wv.loadData( doc , "text/html", "UTF-8");
. Les deux fonctionnent pour moi
Téléchargez le code source à partir d’ici ( Afficher le fichier PDF dans mon application Android )
Ajoutez cette dépendance dans votre note:
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 + "-");
}
}
}
}
Merci!
Les réponses ci-dessus qui dépendent de Google Documents ne fonctionneront que si vous avez une connexion Internet. Une meilleure option serait d’utiliser Android PdfRenderer class.
Vous pouvez essayer un exemple d'application ici .
Eh bien, je pense que la solution est la suivante
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");
En espérant que cela vous aidera :)