googlearchive/android-PdfRendererBasic

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

yehyatt opened this issue · 6 comments

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

Same here

anyone found the answer for this yet ?

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

    }

}

}`

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.

telyo commented

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

`

I am closing this issue/PR, as it has been migrated to the new repo linked above in the comments. Thank you!