Permalink
Browse files

Reduce maximum zoom level to avoid out of memory exception

1 parent 4214300 commit d9d2b26a2d69ffc82a78c47a51dc4c6e14b0d63f @jblough committed Sep 11, 2011
Showing with 68 additions and 34 deletions.
  1. BIN PdfViewer.jar
  2. +68 −34 src/net/sf/andpdf/pdfviewer/PdfViewerActivity.java
View
Binary file not shown.
@@ -21,6 +21,7 @@
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.RectF;
+import android.graphics.Bitmap.Config;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
@@ -59,6 +60,10 @@
private static final int STARTPAGE = 1;
private static final float STARTZOOM = 1.0f;
+ private static final float MIN_ZOOM = 0.25f;
+ private static final float MAX_ZOOM = 3.0f;
+ private static final float ZOOM_INCREMENT = 1.5f;
+
private static final String TAG = "PDFVIEWER";
public static final String EXTRA_PDFFILENAME = "net.sf.andpdf.extra.PDFFILENAME";
@@ -72,13 +77,13 @@
public static final boolean DEFAULTUSEFONTSUBSTITUTION = false;
public static final boolean DEFAULTKEEPCACHES = false;
- private final static int MEN_NEXT_PAGE = 1;
- private final static int MEN_PREV_PAGE = 2;
- private final static int MEN_GOTO_PAGE = 3;
- private final static int MEN_ZOOM_IN = 4;
- private final static int MEN_ZOOM_OUT = 5;
- private final static int MEN_BACK = 6;
- private final static int MEN_CLEANUP = 7;
+ private final static int MENU_NEXT_PAGE = 1;
+ private final static int MENU_PREV_PAGE = 2;
+ private final static int MENU_GOTO_PAGE = 3;
+ private final static int MENU_ZOOM_IN = 4;
+ private final static int MENU_ZOOM_OUT = 5;
+ private final static int MENU_BACK = 6;
+ private final static int MENU_CLEANUP = 7;
private final static int DIALOG_PAGENUM = 1;
@@ -293,13 +298,13 @@ public void run() {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
- menu.add(Menu.NONE, MEN_PREV_PAGE, Menu.NONE, "Previous Page").setIcon(getPreviousPageImageResource());
- menu.add(Menu.NONE, MEN_NEXT_PAGE, Menu.NONE, "Next Page").setIcon(getNextPageImageResource());
- menu.add(Menu.NONE, MEN_GOTO_PAGE, Menu.NONE, "Goto Page");
- menu.add(Menu.NONE, MEN_ZOOM_OUT, Menu.NONE, "Zoom Out").setIcon(getZoomOutImageResource());
- menu.add(Menu.NONE, MEN_ZOOM_IN, Menu.NONE, "Zoom In").setIcon(getZoomInImageResource());
+ menu.add(Menu.NONE, MENU_PREV_PAGE, Menu.NONE, "Previous Page").setIcon(getPreviousPageImageResource());
+ menu.add(Menu.NONE, MENU_NEXT_PAGE, Menu.NONE, "Next Page").setIcon(getNextPageImageResource());
+ menu.add(Menu.NONE, MENU_GOTO_PAGE, Menu.NONE, "Goto Page");
+ menu.add(Menu.NONE, MENU_ZOOM_OUT, Menu.NONE, "Zoom Out").setIcon(getZoomOutImageResource());
+ menu.add(Menu.NONE, MENU_ZOOM_IN, Menu.NONE, "Zoom In").setIcon(getZoomInImageResource());
if (HardReference.sKeepCaches)
- menu.add(Menu.NONE, MEN_CLEANUP, Menu.NONE, "Clear Caches");
+ menu.add(Menu.NONE, MENU_CLEANUP, Menu.NONE, "Clear Caches");
return true;
}
@@ -311,31 +316,31 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
- case MEN_NEXT_PAGE: {
+ case MENU_NEXT_PAGE: {
nextPage();
break;
}
- case MEN_PREV_PAGE: {
+ case MENU_PREV_PAGE: {
prevPage();
break;
}
- case MEN_GOTO_PAGE: {
+ case MENU_GOTO_PAGE: {
gotoPage();
break;
}
- case MEN_ZOOM_IN: {
+ case MENU_ZOOM_IN: {
zoomIn();
break;
}
- case MEN_ZOOM_OUT: {
+ case MENU_ZOOM_OUT: {
zoomOut();
break;
}
- case MEN_BACK: {
+ case MENU_BACK: {
finish();
break;
}
- case MEN_CLEANUP: {
+ case MENU_CLEANUP: {
HardReference.cleanup();
break;
}
@@ -346,10 +351,20 @@ public boolean onOptionsItemSelected(MenuItem item) {
private void zoomIn() {
if (mPdfFile != null) {
- if (mZoom < 4) {
- mZoom *= 1.5;
- if (mZoom > 4)
- mZoom = 4;
+ if (mZoom < MAX_ZOOM) {
+ mZoom *= ZOOM_INCREMENT;
+ if (mZoom > MAX_ZOOM)
+ mZoom = MAX_ZOOM;
+
+ if (mZoom >= MAX_ZOOM) {
+ Log.d(TAG, "Disabling zoom in button");
+ mGraphView.bZoomIn.setEnabled(false);
+ }
+ else
+ mGraphView.bZoomIn.setEnabled(true);
+
+ mGraphView.bZoomOut.setEnabled(true);
+
//progress = ProgressDialog.show(PdfViewerActivity.this, "Rendering", "Rendering PDF Page");
startRenderThread(mPage, mZoom);
}
@@ -358,10 +373,20 @@ private void zoomIn() {
private void zoomOut() {
if (mPdfFile != null) {
- if (mZoom > 0.25) {
- mZoom /= 1.5;
- if (mZoom < 0.25)
- mZoom = 0.25f;
+ if (mZoom > MIN_ZOOM) {
+ mZoom /= ZOOM_INCREMENT;
+ if (mZoom < MIN_ZOOM)
+ mZoom = MIN_ZOOM;
+
+ if (mZoom <= MIN_ZOOM) {
+ Log.d(TAG, "Disabling zoom out button");
+ mGraphView.bZoomOut.setEnabled(false);
+ }
+ else
+ mGraphView.bZoomOut.setEnabled(true);
+
+ mGraphView.bZoomIn.setEnabled(true);
+
//progress = ProgressDialog.show(PdfViewerActivity.this, "Rendering", "Rendering PDF Page");
startRenderThread(mPage, mZoom);
}
@@ -372,7 +397,9 @@ private void nextPage() {
if (mPdfFile != null) {
if (mPage < mPdfFile.getNumPages()) {
mPage += 1;
- progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page");
+ mGraphView.bZoomOut.setEnabled(true);
+ mGraphView.bZoomIn.setEnabled(true);
+ progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true);
startRenderThread(mPage, mZoom);
}
}
@@ -382,7 +409,9 @@ private void prevPage() {
if (mPdfFile != null) {
if (mPage > 1) {
mPage -= 1;
- progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page");
+ mGraphView.bZoomOut.setEnabled(true);
+ mGraphView.bZoomIn.setEnabled(true);
+ progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true);
startRenderThread(mPage, mZoom);
}
}
@@ -427,7 +456,9 @@ public void onClick(DialogInterface dialog, int whichButton) {
catch (NumberFormatException ignore) {}
if ((pageNum!=mPage) && (pageNum>=1) && (pageNum <= mPdfFile.getNumPages())) {
mPage = pageNum;
- progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page");
+ mGraphView.bZoomOut.setEnabled(true);
+ mGraphView.bZoomIn.setEnabled(true);
+ progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true);
startRenderThread(mPage, mZoom);
}
}
@@ -456,6 +487,9 @@ public void onClick(DialogInterface dialog, int whichButton) {
//private TextView mLine3View;
private Button mBtPage;
private Button mBtPage2;
+
+ ImageButton bZoomOut;
+ ImageButton bZoomIn;
public GraphView(Context context) {
super(context);
@@ -471,7 +505,7 @@ public GraphView(Context context) {
vl.setOrientation(LinearLayout.VERTICAL);
if (mOldGraphView == null)
- progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page");
+ progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page", true, true);
addNavButtons(vl);
// remember page button for updates
@@ -554,7 +588,7 @@ private void addNavButtons(ViewGroup vg) {
hl.setOrientation(LinearLayout.HORIZONTAL);
// zoom out button
- ImageButton bZoomOut=new ImageButton(context);
+ bZoomOut=new ImageButton(context);
bZoomOut.setBackgroundDrawable(null);
bZoomOut.setLayoutParams(lpChild1);
//bZoomOut.setText("-");
@@ -568,7 +602,7 @@ public void onClick(View v) {
hl.addView(bZoomOut);
// zoom in button
- ImageButton bZoomIn=new ImageButton(context);
+ bZoomIn=new ImageButton(context);
bZoomIn.setBackgroundDrawable(null);
bZoomIn.setLayoutParams(lpChild1);
//bZoomIn.setText("+");

5 comments on commit d9d2b26

Hi not sure why after downloading the Android-Pdf-Viewer-Library-master zip file and trying to run the PdfViewer, I get
"Android library projects cannot be launched" ?

I have added the PdfViewer.jar to Build Path
and all png files are in res/layout and res/drawables.

In the AndroidManifest.xml file I have selected the .PdfViewerActivity as Main / Launcher Intent filters

So do I need to make another class in order to run it?

Great Tool, Really Simple API....
But shows junk/Garbage inside the PDF instead of words....
Please do fix this...

Its really great to see the library for pdfViewer.... Thanks for posting it.
I am trying to use this library, i am done with all the above steps successfully and when i am running the app getting file not found error.

Intent intent = new Intent(MainActivity.this, ViewPdfInsideApp.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, "your file name ");
startActivity(intent);

Tried giving - pdf file from

  1. raw folder as "application/pdfs/demo.pdf"
  2. from assets folder - "/asstes/pdfs/demo.pdf"
  3. from External file - Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF/demo.pdf"

but getting error ST='file 'application/pdfs/demo.pdf' not found'

Please let me know if i need to write any other code.

Thanks

i did as told but activity showing "loading pdf page for so long

This library is not good because some pdf's are not viewing in proper format look the snapshop of the pdf
so please solve this problem...
screenshot_2015-09-03-09-19-17

Please sign in to comment.