问题描述:

i am trying to implement a pdf reader via a pdf library from git hub https://github.com/jblough/Android-Pdf-Viewer-Library but when i implement the code.. all i am getting is a blank page.. the url is correct the pdf has content and this is similar to this q .. Example of code to implement a PDF reader

my code consist of multiple methods, the main method is used to select the which pdf should be chosen to display. then the pdf name is passed on to method "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); }

the openpdfintentmethod is used to open the values

 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 contains the following..

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

}

网友答案:

In your AndroidManifest.xml file

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

in your case activity is pdf class

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

and use following method

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

and call it as below

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

And the function copyfile is as below

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

The replacement is

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

to

out=new   FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/mypdf.pdf");
相关阅读:
Top