Android Question Using Photos from the Phone

Shadow&Max

Active Member
Licensed User
Longtime User
OK, so I want to retrieve a photo from the phone... I use the ContentChooser to get the photo.

Now, I want to save the location of that picture in a file, and reload it at a later time.

I can GET the picture the first time... That's working. Then I save the Dir and FileName to variables that are saved in the file.

When I reload the file, I want to set an image control to that photo.

I'm getting a Security message when I try to access that file again:
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{429c19b0 13253:com.twodogapps.gaa/u0a136} (pid=13253, uid=10136) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS

The file dir and file name that are being saved are:
Dir: ContentDir
FileName: content://com.android.providers.media.documents/document/image%3A131

The ContentChooser is initialized, I'm pretty sure. I set the image like this:

iv.SetBackgroundImage(LoadBitmap(gs.listhere(Index).PhotoDir, gs.listhere(Index).PhotoFile))

In the above line, the index is correct and is getting the values above. As soon as it hits this line, I get the SecurityException shown above.

What am I doing wrong here?

Also, on my phone's permissions for the app it says:

  1. Modify or delete the contents of your USB storage
  2. full network access
  3. access Bluetooth settings - Pair with Bluetooth devices
  4. read the contents of your USB storage.
As far as I can tell those are all I need in terms of permissions.

I don't want to save the file in my apps database because they're just too big and no need to waste duplicate space.
 
Last edited:

hibrid0

Active Member
Licensed User
Longtime User
I try your code, And I dont use SQL in my App, I add SQL Library and say "java.lang.RuntimeException: Object should first be initialized (Cursor)."



 
Upvote 0

Marco Nissen

Active Member
Licensed User
Longtime User
This means that the file path is not available. Note that in most cases you do not need this path to work with the selected image.

Well, it seems as if the file path is a security issue for the latest update of Android. Instead, you should use the input stream via content resolver / openinputstream .. small snippet:

B4X:
Sub GetStreamFromContentResult(UriString As String) As InputStream
    Dim r As Reflector
    Dim Uri As Object
    Dim iStream As InputStream
    Uri = r.RunStaticMethod("android.net.Uri", "parse", Array As Object(UriString), Array As String("java.lang.String"))
    r.Target = r.GetContext
    r.Target = r.RunMethod("getContentResolver")
    iStream = r.RunMethod4( "openInputStream", Array As Object(Uri), Array As String("android.net.Uri"))
    Return iStream
End Sub

With this, you can easily receive e.g. a PDF

B4X:
Try
                         Dim theTitle As String =getTitleFromContent(ShareString)  ' the approach above using _display_name
                        Dim thePath As String = ah.getTemporaryDirectory ' my way to obtain a writeable path
                        Dim jo As JavaObject
                        Dim Inp As InputStream  = GetStreamFromContentResult(ShareString)
                        Dim Out As OutputStream = File.OpenOutput(thePath, theTitle, False)
                        File.copy2(Inp, Out)
                        Inp.Close
                        Out.Close
                                                receivedPDF = thePath & "/" & theTitle
                    Catch
                        Log(":: issue " & LastException)
                    End Try
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…