Bug 23351 - Exception while using OpenFd on pdf files
Summary: Exception while using OpenFd on pdf files
Status: RESOLVED INVALID
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.16.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 4.20
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2014-09-24 14:22 UTC by John Pilczak
Modified: 2014-10-30 17:04 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description John Pilczak 2014-09-24 14:22:57 UTC
I have been trying to port the new PdfRendererBasic Android L sample but have run into an issue opening the file that was not run into in the original java code. When I call OpenFd(sample.pdf) I get a Java.io.FileNotFoundException that says "This file can not be opened as a file descriptor; it is probably compressed." This is done the exact same way in the java code and the PdfRenderer constructor has no overloads to take anything but a ParcelFileDescriptor so unless I am mistaken, the only way to get this to work is to allow OpenFd() to work on pdf files. I have attached my ported code at this point.

Context of exception:
private void OpenRenderer(Context context)
{
     fileDescriptor = context.Assets.OpenFd("sample.pdf").ParcelFileDescriptor; //EXCEPTION HERE
     pdfRenderer = new PdfRenderer (fileDescriptor);
}

Application output: https://gist.github.com/JohnPilczak/8864a7e00e38f0a9cdf3

Environment Details: 
=== Xamarin Studio ===

Version 5.5 (build 198)
Installation UUID: ea2dde6b-0915-4e87-bf59-0163cddeada5
Runtime:
	Mono 3.10.0 ((detached/ac51002)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000016

=== Apple Developer Tools ===

Xcode 6.0 (6299)
Build 6A313

=== Xamarin.Mac ===

Version: 1.11.0.1 (Starter Edition)

=== Xamarin.Android ===

Version: 4.17.0.10 (Business Edition)
Android SDK: /Users/johnpilczak/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		1.6    (API level 4)
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.0    (API level 11)
		3.1    (API level 12)
		3.2    (API level 13)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		4.5    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin.iOS ===

Version: 8.2.0.176 (Business Edition)
Hash: 973f8d4
Branch: 
Build date: 2014-09-17 17:40:20-0400

=== Build Information ===

Release ID: 505000198
Git revision: 7495942eb76d6b80c460ddd61f2b94cba1a97fa2
Build date: 2014-09-18 09:50:12-04
Xamarin addins: c571b625445d60f2c8b189b309a6ffc87386caed

=== Operating System ===

Mac OS X 10.9.4
Darwin Vinces-xMac.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 1 John Pilczak 2014-09-24 14:27:12 UTC
My ported sample code: https://github.com/xamarin/private-samples/tree/master/android-l/PdfRendererBasic
Comment 2 Shruti 2014-09-26 08:05:04 UTC
I have tried to check this issue. I got this sample from github path given in comment1. But I have observed that PdfRenderer class is missing  in Mono.Android. Due to this, I am not able to check the issue. Here is the screencast regarding this : http://screencast.com/t/N8ruYdvWvdw0

Please let me know Should I report other issue regarding this.  Looking for your suggestions !!

Environment Info :
=== Xamarin Studio ===

Version 5.4 (build 240)
Installation UUID: 67eaf3b4-f8a7-4ab4-a9df-3197350ca5dc
Runtime:
	Mono 3.8.0 ((no/db71bc1)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 308000013

=== Apple Developer Tools ===

Xcode 6.0.1 (6528)
Build 6A317

=== Xamarin.Android ===

Version: 4.16.0 (Business Edition)
Android SDK: /Users/ixamarin78/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		3.2    (API level 13)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		4.5    (API level 21)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Xamarin.iOS ===

Version: 8.0.0.63 (Business Edition)
Hash: 58b46df
Branch: 
Build date: 2014-09-18 22:32:07-0400

=== Xamarin.Mac ===

Version: 1.10.0.7 (Business Edition)

=== Build Information ===

Release ID: 504000240
Git revision: 01786bc67c7024ec33d327ed27e4416d7a846f4e
Build date: 2014-09-17 10:58:48-04
Xamarin addins: 7cd7dfcd6b7b7b53281508954ec080f1cd153ad3

=== Operating System ===

Mac OS X 10.9.4
Darwin XamnewiMac.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 3 John Pilczak 2014-09-26 10:04:40 UTC
I believe in order for the class to appear you would need Xamarin.Android 4.17 because it is a class that is new in Android L. You should be able to get the version I have from the Alpha channel.
Comment 4 Jonathan Pryor 2014-10-30 17:04:27 UTC
The error message in the Application output, via Comment #0:

> [MonoDroid] java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

The obvious question: should the file be compressed?

Let us turn our attention to the source Java project:

https://github.com/googlesamples/android-PdfRendererBasic

Does it do anything to disable compression of PDF files?

https://github.com/googlesamples/android-PdfRendererBasic/blob/master/PdfRendererBasicSample/build.gradle#L50
>     aaptOptions {
>         noCompress "pdf"
>     }

So... "Yes."

Meanwhile, in the C# project:

https://github.com/xamarin/private-samples/blob/master/android-l/PdfRendererBasic/PdfRendererBasic/PdfRendererBasic.csproj

There is no $(AndroidStoreUncompressedFileExtensions) property definition.

So what's going on is the upstream Java sample is configured so that all .pdf files are *stored* uncompressed in the .apk, which the getParcelFileDescriptor() method requires. The C# project, meanwhile, has not performed this change, and thus the .pdf IS compressed in the .apk, causing the ParcelFileDescriptor property to throw.

The fix? Set the $(AndroidStoreUncompressedFileExtensions) MSBuild property. Within Xamarin Studio, this can be done in Project Options > Build / Android Build > General tab > Uncompressed resource extensions.