bitrock
bitrock

Reputation: 1354

E/PdfViewerActivity: fetchFile:file: java.io.FileNotFoundException: file does not exist

I'm trying to open a pdf after creating it via an intent. The file exists and is readable but save inside the apps directory.

Currently the file is saved in the following manner

        OutputStream out;
    try {
        //TODO: expose through a content provider
        out = mContext.openFileOutput(outputFileName, Context.MODE_WORLD_READABLE);
        document.writeTo(out);
        out.close();

and the file is sent to an intent with the following code

    final Intent viewIntent = new Intent();
    viewIntent.setAction(Intent.ACTION_VIEW);
    viewIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
    mActivity.startActivity(viewIntent);

The app trying to open it is the google drive viewer and I see in the Android monitor

E/PdfViewerActivity: fetchFile:file: java.io.FileNotFoundException: file does not exist

This previously worked and I don't think I've changed anything related to this code however I have updated my tooling. I've tried changing the buildToolsVersion in the build.gradle back to what it was before as well as the support libraries 'com.android.support:appcompat-v7:22.+' and 'com.android.support:design:22.+' instead of version 24.2.1

I've tried using the debugger to set the file to be readable just before sending the intent but that did not work.

Thanks

Upvotes: 1

Views: 919

Answers (1)

bitrock
bitrock

Reputation: 1354

I found the answer starting with the Android 7 Behavior Changes documentation. Sharing files is no longer allowable when setting MODE_WORLD_READABLE even though the documentation doesn't mention any of this

enter image description here

I followed the documentation on sharing files to fix the issue but the sample code in the documentation was more complicated than what I needed. I still had to add the file provider to the AndroidManifest.xml as well as create the fileprovider.xml.

The resulting code is now

    final Intent viewIntent = new Intent();
    viewIntent.setAction(Intent.ACTION_VIEW);
    Uri fileUri = FileProvider.getUriForFile(mActivity, "my.authority.fileprovider", file);
    viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    viewIntent.setDataAndType(fileUri, "application/pdf");

Upvotes: 2

Related Questions