1

I have taken over an outdated Android App written in Java

I have progressively upgraded dependencies without issue until I changed the targetSdkVersion from 27 to 30.

This is for a feature that:

  1. Allows user to pick an from the gallery or capture from camera
  2. Saves a value to an application folder
  3. Sends the picture to an API

I know that pre-Android 10, I could add android:requestLegacyExternalStorage="true" in the Manifest file and it would work, but that feature was from Android 10+.

If I downgrade the targetSdkVersion, it works.

Otherwise it throws the following errors:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/xxxxx/1380402_672833029402941_703996746_n.jpg: open failed: EACCES (Permission denied)

I have narrowed it down to a permissions issue, due to scoped storage restrictions

I understand the concept behind it but I cannot get it to work.

What do I need to do to fix this in Java code, as I don't know how to apply the examples in Kotlin for Java?

I have tried following some of the answers from below:

Sample app: https://github.com/tinonetic/android-questions-scoped-storages

  1. Write permissions not working - scoped storage Android SDK 30 (aka Android 11)
  2. Scoped Storage android 10.0
  3. Add image to Media Gallery - Android
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
tinonetic
  • 7,751
  • 11
  • 54
  • 79
  • 2
    `Allows user to pick an from the gallery or capture from camera` gives you a content `Uri`. Use that. Copy from the Uri input stream to a file on your app's private storage. There you can do anything you want. – Eugen Pechanec Apr 10 '21 at 22:10
  • "but I cannot get it to work" -- then provide a [mcve] and explain in detail what problems you encountered. – CommonsWare Apr 10 '21 at 22:16
  • Thank-you @EugenPechanec to where do I copy the input stream? – tinonetic Apr 10 '21 at 22:21
  • @CommonsWare Thanks, I have included a link to a GitHub repository with an app that tries to do what I am trying to do – tinonetic Apr 10 '21 at 22:24
  • 2
    Link to a github repository is not a viable sample. Don't expect people to sift through entire project. – Pawel Apr 10 '21 at 22:34
  • More importantly, we do not know what "cannot get it to work" means. You should start by getting rid of `extractPhysicalPathFromUri()` and `getImagePath()`. They are both unreliable and are unnecessary. `BitmapFactory` has `decodeStream()`, and you can get an `InputStream` on the content by calling `getContentResolver().openInputStream()`, supplying your `Uri`. Even better would be to use an image-loading library, such as Glide or Picasso, and save yourself hundreds of lines of code (not to mention the icky disk I/O on the main application thread). – CommonsWare Apr 10 '21 at 23:03
  • Try this: https://github.com/anggrayudi/SimpleStorage – Anggrayudi H Jul 03 '21 at 19:57

0 Answers0