Prajwal Waingankar
Prajwal Waingankar

Reputation: 2728

Fatal Exception: java.lang.IllegalArgumentException fd cannot be null in onwrite() of PrintAdapter class in Android Q only

I suddenly faced this issue when I tested this on Android Q device for the Print PDF feature. Its was working fine till Android P. I tried finding solution on the internet but of no use. Any help would be good.

Upvotes: 3

Views: 2278

Answers (2)

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2728

I got the solution to this by refering the official documentation here: Android 10 Privacy

We have to add:

android:requestLegacyExternalStorage="true"

in the application tag of the manifest file for versions >= 29.

The documentation:

If {@code true} this app would like to run under the legacy storage model. Note that this may not always be respected due to policy or backwards compatibility reasons. Apps not requesting legacy storage can continue to discover and read media belonging to other apps via {@code MediaStore}. The default value is: - {@code false} for apps with targetSdkVersion >= 29 (Q). - {@code true} for apps with targetSdkVersion 29.

Although, from the article is has stated that from Android 11, the scoped storage will be introduced and therefore from Android 11, this work around wont be possible.

here,

To give developers additional time for testing, apps that target Android 10 (API level 29) can still request the requestLegacyExternalStorage attribute.

So you will have to mandatorily jump to scoped storage for >= Android 11.

Hope this solution helps someone.

====================

Solution 2:

If above didnt worked than the only reason is that you haven't provided Read/Write permission in your android app. Add permission logic in code and reinstall app -> give permission and it works!

Upvotes: 0

Jr'Z
Jr'Z

Reputation: 1

I've had this issue when upgrading to API Level 31.

It came from the fact that, in the pdfprint class I had, there was a line of code with a :

if (file.createNewFile()) {
    return ParcelFileDescriptor.open(file, 

ParcelFileDescriptor.MODE_READ_WRITE); }

By adding a code that removed the file before creating it, it just worked fine

if (file.exists()) file.delete();

if file.createNewFile()
    return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);

The code I used was based on one I found on Internet, so I did not take time to get the opportunity to clean it up, but it's what solved that issue to me.

Upvotes: 0

Related Questions