sandamali perera
sandamali perera

Reputation: 156

It doesntn't show pdf file

I used pdf view example from github and try to run.

https://github.com/JoanZapata/android-pdfview

below is the main class

package com.joanzapata;

import android.content.Intent;
import android.util.Log;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.googlecode.androidannotations.annotations.*;
import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnPageChangeListener;


import static java.lang.String.format;


public class PDFViewActivity extends SherlockActivity implements OnPageChangeListener {

    public static final String SAMPLE_FILE = "sample.pdf";

    public static final String ABOUT_FILE = "about.pdf";

    @ViewById
    PDFView pdfView;

    @NonConfigurationInstance
    String pdfName = SAMPLE_FILE;

    @NonConfigurationInstance
    Integer pageNumber = 1;

    @AfterViews
    void afterViews() {
        display(pdfName, false);
    }

    @OptionsItem
    public void about() {
        if (!displaying(ABOUT_FILE))
            display(ABOUT_FILE, true);
    }

    private void display(String assetFileName, boolean jumpToFirstPage) {
        if (jumpToFirstPage) pageNumber = 1;
        setTitle(pdfName = assetFileName);

        pdfView.fromAsset(assetFileName)
                .defaultPage(pageNumber)
                .onPageChange(this)
                .load();
    }

    @Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
        setTitle(format("%s %s / %s", pdfName, page, pageCount));
    }

    @Override
    public void onBackPressed() {
        if (ABOUT_FILE.equals(pdfName)) {
            display(SAMPLE_FILE, true);
        } else {
            super.onBackPressed();
        }
    }

    private boolean displaying(String fileName) {
        return fileName.equals(pdfName);
    }
}

I have already attached actionbarsherlock and androidannotations-2.5-api jar files to project.This run without any errors,but it doesn't show pdf file.

Upvotes: 3

Views: 2655

Answers (2)

Jaydev
Jaydev

Reputation: 1812

This would work:

pdfView = (PDFView) findViewById(R.id.pdfView);
pdfView.fromFile(new File(path)).defaultPage(pageNumber).onPageChange(this).load();

Upvotes: 1

Patryk Tchaikovsky
Patryk Tchaikovsky

Reputation: 11

Do you have a file named "sample.pdf" in your assets folder ? Where do I place the 'assets' folder in Android Studio?

You can try also read file from sd card:

    file = new File(path); // where "path" is a path to file on your sd card, example: /storage/sdcard0/yourFile.pdf
    filename = file.getName();
    setTitle(filename);
    pdfView  =(PDFView)findViewById(R.id.pdfView); 
    pdfView.fromFile(file).defaultPage(1).showMinimap(false).enableSwipe(true).swipeVertical(true).load();
    pdfView.setVerticalScrollBarEnabled(true);

Upvotes: 1

Related Questions