graphics-samples icon indicating copy to clipboard operation
graphics-samples copied to clipboard

java.io.IOException: cannot create document. Error: 3

Open codingjeremy opened this issue 4 years ago • 6 comments

Issue by yehyatt Tuesday Jan 31, 2017 at 12:40 GMT Originally opened as https://github.com/googlesamples/android-PdfRendererBasic/issues/15


When I download the sample and try to run it this is the error I get :

ClassLoader referenced unknown path: /data/app/com.example.android.pdfrendererbasic-1/lib/arm java.io.IOException: cannot create document. Error: 3

    mPdfRenderer = new PdfRenderer(mFileDescriptor);

mPdfRenderer is always null

codingjeremy avatar Sep 06 '19 17:09 codingjeremy

Comment by vijaypwr61 Saturday Feb 18, 2017 at 17:31 GMT


Same here

codingjeremy avatar Sep 06 '19 17:09 codingjeremy

Comment by Mowahed Sunday Apr 23, 2017 at 11:00 GMT


anyone found the answer for this yet ?

codingjeremy avatar Sep 06 '19 17:09 codingjeremy

Comment by vijaypwr61 Monday Apr 24, 2017 at 08:56 GMT


I'm using this nugget package => Xamarin.PdfView.Android.1.0.4

and I found code for Displaying pdf files instead using above [PdfRendere].

`using Com.Joanzapata.Pdfview;

namespace Physics { [Activity(Label = "Physics",Theme ="@style/MyTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

public class PdfPageViewer : Activity
{
    private PDFView PdfPageView;

    protected override void OnCreate(Bundle savedInstanceState)
    {


        base.OnCreate(savedInstanceState);
        Window.AddFlags(Android.Views.WindowManagerFlags.Fullscreen);
        Window.RequestFeature(Android.Views.WindowFeatures.NoTitle);

        SetContentView(Resource.Layout.PdfViewerPageLayout);

        string FilePath2 = Intent.GetStringExtra("FilePath2");
        string FilePath3 = Intent.GetStringExtra("FilePath3");

        PdfPageView = FindViewById<PDFView>(Resource.Id.PdfPageView);
        if (!string.IsNullOrEmpty(FilePath2))
        {
            PdfPageView.FromAsset(FilePath2+ ".pdf").Load();
        }
        else
        {
            PdfPageView.FromAsset(FilePath3+".pdf").Load();
        }

    }

}

}`

codingjeremy avatar Sep 06 '19 17:09 codingjeremy

Comment by sdbrannum Thursday Apr 27, 2017 at 22:31 GMT


You need to make sure the file is in the cache as the PdfRenderer can't handle the compressed file directly. You accomplish this by:

try {
    String FILENAME = "WhateverFileYoureLookingFor.pdf";
    FileInputStream input;
    FileOutputStream output;
    final byte[] buffer = new byte[1024];
    int size;
	
    // Create file object to read and write on
    File file = new File(context.getCacheDir(), FILENAME);
	
    // check if this object exists in the cache already
    if (!file.exists()) {        

        // external directory, documents, download respectively
        // input = new FileInputStream(new File(Environment.getExternalStorageDirectory() + "/" + FILENAME));
	// input = new FileInputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/" + FILENAME));
		
        input = new FileInputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + FILENAME));
        output = new FileOutputStream(file);

	// read and write into the cache directory	
        while ((size = input.read(buffer)) != -1) {
            output.write(buffer, 0, size);
        }

	// close the streams
	input.close();
        output.close();
		
        mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
        mPdfRenderer = new PdfRenderer(mFileDescriptor);
    }

} catch (IllegalStateException | IOException | NullPointerException e) {
    e.printStackTrace();
}

For testing sake you may want to clear your cache out at the start of this try every time. You can see how to do that in this stackoverflow question.

codingjeremy avatar Sep 06 '19 17:09 codingjeremy

Comment by telyo Monday Aug 20, 2018 at 09:05 GMT


my problem is ` //if file.length ==0 while throw this exception : java.io.IOException: cannot create document. Error: 3 //so best to check yours mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); mPdfRenderer = new PdfRenderer(mFileDescriptor);

`

codingjeremy avatar Sep 06 '19 17:09 codingjeremy

Is this issue fixed ? Is there any workaround for this issue ? I'm getting this exception Caused by java.io.IOException: cannot create document. Error: 3 at android.graphics.pdf.PdfRenderer.nativeCreate(PdfRenderer.java) at android.graphics.pdf.PdfRenderer.(PdfRenderer.java:165) at com.orbi.eka.files.pdf.PDFAdapter.(PDFAdapter.java)

pradeep-orbi avatar Aug 11 '21 05:08 pradeep-orbi