0

My app has "Files and media: Allow management of all files" permission. However, when I try to open a PDF file LogCat reports the following errors:

2022-04-20 21:06:30.573 4134-6853/? E/MediaProvider: Permission to access file: /storage/emulated/0/MapDatabase/GuidesPdf/Church guides/Broadwater, Sussex guide book.pdf is denied 2022-04-20 21:06:30.573 24666-26342/? E/DisplayData: openFd: java.io.FileNotFoundException: open failed: EACCES (Permission denied) 2022-04-20 21:06:30.574 24666-26342/? E/PdfLoader: Can't load file (doesn't open) Display Data [PDF : Broadwater, Sussex guide book.pdf] +FileOpenable, uri: file:///storage/emulated/0/MapDatabase/GuidesPdf/Church%20guides/Broadwater%2C%20Sussex%20guide%20book.pdf, has subtitles: false

The code to display the PDF is:

                File pdfFile = new File(Environment.getExternalStorageDirectory().getPath(),"/MapDatabase/GuidesPdf/"+strFolderToShow+"/"+strFileToShow);
                Uri path = Uri.fromFile(pdfFile);
                Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                pdfIntent.setDataAndType(path, "application/pdf");
                pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(pdfIntent);

I've updated the Min, Compile and TargetSdk versions to match the devices that I want the app to run on now. The build.gradle (Module:app) file now reads as follows:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.prepbgg.mymap"
        minSdkVersion 24
        targetSdkVersion 29
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile files('libs/jcoord-1.0.jar')
    implementation files('libs/jcoord-1.0.jar')
    implementation "androidx.core:core:1.7.0"
}
prepbgg
  • 3,564
  • 10
  • 39
  • 51
  • 2
    You should be crashing with a `FileUriExposedException`. `Uri.fromFile()` has been largely unusable for six years. Use `FileProvider` and its `getUriForFile()` to get a `Uri` for serving a file to a third-party app. – CommonsWare Apr 20 '22 at 20:26
  • Thanks for pointing me to FileProvider. This is an app for my own personal use that I've been using happily on an old phone until it died. It's a shame that Google doesn't always allow backwards compatibility. However, I think I understand the reasons in this case. Luckily, I don't encounter this kind of issue too often. Besides, updating the app helps to keep the brain cells young! I shall now have to see whether I can get it to work on the newer phone. :) – prepbgg Apr 22 '22 at 07:21
  • @CommonsWare The first step in https://developer.android.com/reference/androidx/core/content/FileProvider is to add a `provider` tag to the manifest. When I copy the code from the documentation Android Studio highlights `core.content.FileProvider` in red and says that the `core`, `content` and `FileProvider` packages are unresolved. I have changed `mydomain` in the `authorities` line to the pseudo domain that I use to name my personal Android apps. I can't see anything in the FileProvider documentation to help. How do I resolve this error? – prepbgg Apr 22 '22 at 08:26
  • 1
    Everything in an `androidx` package comes from a library. `FileProvider` comes from `androidx.core:core`. – CommonsWare Apr 22 '22 at 10:49
  • @CommonsWare Thanks again for your advice. It looks as if I have a steep learning curve ahead of me since I know nothing about androidx. Two immediate questions: (1) https://developer.android.com/jetpack/androidx says that I shall "need to set the compile SDK to Android 9.0 (API level 28)". Does this mean that I will no longer be able to use the app on a device running Android 7.0? (2) You say that I shall need the `androidx.core:core` library, but I don't see that in the packages listed in https://developer.android.com/reference/androidx/packages – prepbgg Apr 22 '22 at 19:15
  • Am I right in thinking that I shall need to include references to androidx in one of my gradle.build files? https://developer.android.com/reference/androidx/core/content/FileProvider seems to set out very clearly what I need to do in the manifest and the paths xml file and the program code, but provides no help with gradle. Is it possible to point me to a document that sets out (in simple terms) what I need to do? – prepbgg Apr 22 '22 at 19:22
  • "Does this mean that I will no longer be able to use the app on a device running Android 7.0?" -- `compileSdkVersion` is unrelated to your concern. `minSdkVersion` is what you are thinking of, and most of the Jetpack supports back to Android 4.x, last I checked. "I don't see that in the packages" -- those are Java package names, not library names. "Am I right in thinking that I shall need to include references to androidx in one of my gradle.build files?" -- normally, you would already have some, as the Jetpack is five years old or so at this point. – CommonsWare Apr 22 '22 at 19:27
  • @CommonsWare Thank you for your patience! Am I right in thinking that I need to amend the build.gradle file(s) to "resolve" the missing packages (`core` and `content`) and class (`FileProvider`). I've edited the question to include the content of the `build.gradle (Module:app)` file. Do I need to add one or more `dependencies`? Can you advise exactly what I need to change, please? (I've found a suggestion that cleaning and rebuilding the project might fix the problem, but that hasn't worked.) – prepbgg Apr 23 '22 at 15:11
  • "Do I need to add one or more dependencies?" -- yes. "Can you advise exactly what I need to change" -- if all you want is `FileProvider`, add `implementation "androidx.core:core:1.7.0"` to your module's `dependencies` closure. – CommonsWare Apr 23 '22 at 15:31
  • Thanks again for all your support. (I also had to add a new `gradle.properties` file containing the line `android.useAndroidX=true`) – prepbgg Apr 23 '22 at 16:38
  • @CommonsWare I really appreciate all your help! The latest error arises when I try to build the app: the build fails with an Android resource linking error `AAPT: error: resource android:attr/lStar not found.` and the `` tag within `values.xml` is highlighted. I've installed Android 10.0 in the SDK Manager to match the Target and Compile versions. I've amended the question again to show the current content of `build.gradle`. Is the problem now perhaps with `buildToolsVersion`? – prepbgg Apr 24 '22 at 11:02
  • I have no idea, sorry. – CommonsWare Apr 24 '22 at 11:07
  • Following advice on https://stackoverflow.com/questions/69033022/message-error-resource-androidattr-lstar-not-found I tried installing SDK version 31 and making this the Target and Compile version. The app now compiles and installs but immediately crashes when trying to load a jpg file from external storage. The app has Files and Media access permission to "Allow access to media only". I wonder whether the rules have changed in higher API levels regarding access by the app to files. I'll post a separate question to see if anyone can help with this! – prepbgg Apr 24 '22 at 15:02

0 Answers0