获得关于在项目中实施pdf阅读器的空白pdf

时间:2014-03-18 06:19:06

标签: java android pdf

我正在尝试通过git hub https://github.com/jblough/Android-Pdf-Viewer-Library的pdf库实现pdf阅读器,但是当我实现代码时...我得到的只是一个空白页面..网址是正确的,pdf有内容和这类似于这个q .. Example of code to implement a PDF reader

我的代码由多个方法组成,main方法用于选择应该选择显示哪个pdf。然后将pdf名称传递给方法" copyreadassets"

public void CopyReadAssets(String url) {
      AssetManager assetManager = getApplicationContext().getAssets();
      InputStream in = null;
      OutputStream out = null;
      File file = new File(getApplicationContext().getFilesDir(), url);
      try {
               in = assetManager.open(url);
               out = getApplicationContext().openFileOutput(file.getName(),
               Context.MODE_WORLD_READABLE);

               copyFile(in, out);
               in.close();
               in = null;
               out.flush();
               out.close();
               out = null;
      } 
      catch (Exception e) {
       Log.e("tag", e.getMessage());
      }
      String path = "file://" + getApplicationContext().getFilesDir() + "/"+url;
      openPdfIntent(path);  }

openpdfintentmethod用于打开值

  private void openPdfIntent(String path) {
    // TODO Auto-generated method stub
     try {
            final Intent intent = new Intent(Question_Point_Main.this, Pdf.class);
            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
}

pdf.class包含以下内容..

public class Pdf extends Activity{

public int getPreviousPageImageResource() {
    return R.drawable.left_arrow;  }
public int getNextPageImageResource() {
    return R.drawable.right_arrow;  }
public int getZoomInImageResource() {
    return R.drawable.zoom_in; }
public int getZoomOutImageResource() {
    return R.drawable.zoom_out;  }
public int getPdfPasswordLayoutResource() {
    return R.layout.pdf_file_password;  }
public int getPdfPageNumberResource() {
    return R.layout.dialog_pagenumber; }
public int getPdfPasswordEditField() {
    return R.id.etPassword;  }
public int getPdfPasswordOkButton() {
    return R.id.btOK;  }
public int getPdfPasswordExitButton() {
    return R.id.btExit; }
public int getPdfPageNumberEditField() {
    return R.id.pagenum_edit; }

}

1 个答案:

答案 0 :(得分:0)

AndroidManifest.xml 文件

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


 <activity android:name="com.example.readassetpdf.myPDFActivity"></activity>

您的案例活动是 pdf class

 <activity android:name="com.example.readassetpdf.pdf"></activity>

并使用以下方法

public void CopyReadAssets(String url) {
      AssetManager assetManager = getApplicationContext().getAssets();
      InputStream in = null;
      OutputStream out = null;
      File file = new File(getApplicationContext().getFilesDir(), url);
      try {
               in = assetManager.open(url);
               //out = getApplicationContext().openFileOutput(file.getName(),
               //Context.MODE_WORLD_READABLE);
               out=new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/mypdf.pdf");
               copyFile(in, out);
               in.close();
               in = null;
               out.flush();
               out.close();
               out = null;
      } 
      catch (Exception e) {
       Log.e("tag", e.getMessage());
      }
      String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/mypdf.pdf";
      openPdfIntent(path);  
      }

并将其命名为

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    CopyReadAssets("mypdf.pdf");
}

功能copyfile如下

   private void copyFile(InputStream in, OutputStream out)  throws IOException{
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

替换是

out = getApplicationContext().openFileOutput(file.getName(),
Context.MODE_WORLD_READABLE);

out=new   FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/mypdf.pdf");