FileNotFoundException using PDFView Library



  • Using this library https://github.com/JoanZapata/android-pdfview To read the PDF, but I can't do this, it makes a mistake.

    public class MaterialsActivity extends AppCompatActivity implements OnPageChangeListener {
        PDFView pdfView;
        Integer pageNumber = 1;
        public static final String SAMPLE_FILE =  Environment.getExternalStorageDirectory().toString() + "/12345.pdf";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_materials);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            PDFView pdfView = (PDFView) findViewById(R.id.pdfview);
            pdfView.fromAsset(SAMPLE_FILE)
                    .defaultPage(pageNumber)
                    .onPageChange(this)
                    .load();
        }
        @Override
        public void onPageChanged(int page, int pageCount) {
            pageNumber = page;
        }
    }
    

    File exists (from the Internet), but the application will collapse when form is created. Don't tell me how to get this thing to work? What am I doing wrong?

    Failures

    Caused by: java.io.FileNotFoundException: /storage/emulated/0/12345.pdf
    at android.content.res.AssetManager.openAsset(Native Method)
    at android.content.res.AssetManager.open(AssetManager.java:316) 
    at android.content.res.AssetManager.open(AssetManager.java:290) 
    at com.joanzapata.pdfview.util.FileUtils.fileFromAsset(FileUtils.java:33)
    at com.joanzapata.pdfview.PDFView.fromAsset(PDFView.java:963) 
    at developervision.ru.novgu.MaterialsActivity.onCreate(MaterialsActivity.java:34)
    

    Added the test File file = new File(SAMPLE_FILE); if(file.exists()) but it passes, i.e. the file exists.

    File of Manifestos

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


  • You've got a problem with you trying to load the file out of. Assets (internal protected folder for Annex files) fromAssetswhen it's not there, it's at the root of the device, that's where you downloaded it. So you need to use the method. fromFile(File file) Here we go.

    File file = new File(SAMPLE_FILE);
    pdfView.fromFile( file )
                .defaultPage(pageNumber)
                .onPageChange(this)
                .load();
    

    Also, maybe you just don't have permission in the manifest to read the files.

    Add this.

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

    In the original Manifest Gate. It should look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest
        ...>
    
    &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;
    
    &lt;application
        ...&gt;
           ....
    &lt;/application&gt;
    

    </manifest>



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2