Android Question Using Photos from the Phone

Discussion in 'Android Questions' started by Shadow&Max, Feb 3, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    ContentResolver
     
  2. mohsen nasrabady

    mohsen nasrabady Active Member Licensed User

    how can get filename and filepath from the getPath sub posted by erel?
     
    Last edited: Nov 3, 2014
  3. mohsen nasrabady

    mohsen nasrabady Active Member Licensed User

    solved with this code
    Code:
    Sub cs_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then  
    Dim fp As String = GetPathFromContentResult(FileName)
    name = fp.SubString2(fp.LastIndexOf(
    "/")+1,fp.Length)
    path = fp.SubString2(0,fp.LastIndexOf("/"))
    End If
    End Sub
     
  4. DonManfred

    DonManfred Expert Licensed User

    Did you see the SUBs here? Maybe they are of help in other situations ;-)
     
  5. mohsen nasrabady

    mohsen nasrabady Active Member Licensed User

    yes i tryed use direct result Dir , FileName for copy but error
     
  6. lymey

    lymey Active Member Licensed User

    Is it possible to get the path / or file from Google Dive also? I am using the content chooser to get a file, and the Uri string looks like:
    Code:
    content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D3
    I suppose the real question is - can you programmatically access a file on Google Drive using the content chooser?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You should be able to open an InputStream to this url.
     
  8. lymey

    lymey Active Member Licensed User

    I have never tried this before, and can't find a good example. But this is what I did:
    Code:
    Sub GetPathFromContentResult(UriString As StringAs String
      
    If UriString.StartsWith("/"Then Return UriString 'If the user used a file manager to choose the image
       
    If UriString.StartsWith("content://com.google.android.apps.docs.storage/document"Then
      
    Dim jo As JavaObject
      jo = jo.InitializeStatic(
    "anywheresoftware.b4a.objects.streams.File").GetField("ContentDir")
       
      
    Dim In As InputStream = File.OpenInput(jo, UriString)
    'the following to see if inputstream works:    
      Dim xbmp As Bitmap
      xbmp.Initialize2(
    In)
      pic.SetBackgroundImage(xbmp)
    'need to save file (with the same file name displayed in Google Drive
    End If
     
    End Sub
    and this is the error I got:
    Obviously I am doing something wrong! Something to do with the way the java object is initialized ...but I am in uncharted territory for me!

    Also how do I resolve the filename from Google Drive using the content chooser?
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Try this:
    Code:
    Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
     
    If Success Then
      
    Dim in As InputStream = File.OpenInput(Dir, FileName)
      ...
     
    End If
    End Sub
     
  10. lymey

    lymey Active Member Licensed User

    That's great I can get the file content - thank you.
    But how do I resolve the file name from Google Drive if I am using the content chooser?
     
  11. lymey

    lymey Active Member Licensed User

    This is what happens using Google Drive with the ContentChooser with the following code:
    Code:
    Sub cc_Result (Success As Boolean, Dir As String, FileName As String)
        
    Log("Success = " & Success & ", Dir = " & Dir & ", FileName = " & FileName)
        
    Dim realPath As String = GetPathFromContentResult(FileName)
        
    Log("realPath = " & realPath)
        
    Activity.SetBackgroundImage(LoadBitmapSample("", realPath, 100%x100%y))
    End Sub

    Sub GetPathFromContentResult(UriString As StringAs String
       
    If UriString.StartsWith("/"Then Return UriString 'If the user used a file manager to choose the image
       Dim Cursor1 As Cursor
       
    Dim Uri1 As Uri
       
    Dim Proj() As String = Array As String("_data")
       
    Dim cr As ContentResolver
       cr.Initialize(
    "")
       
    If UriString.StartsWith("content://com.android.providers.media.documents"Then
         
    Dim i As Int = UriString.IndexOf("%3A")
         
    Dim id As String = UriString.SubString(i + 3)
         Uri1.Parse(
    "content://media/external/images/media")
         Cursor1 = cr.Query(Uri1, Proj, 
    "_id = ?"Array As String(id), "")
       
    Else
         Uri1.Parse(UriString)
         Cursor1 = cr.Query(Uri1, Proj, 
    ""Null"")
       
    End If
       Cursor1.Position = 
    0
       
    Dim res As String
       res = Cursor1.GetString(
    "_data")
       Cursor1.Close
       
    Return res
    End Sub
    An error is generated at :
    Code:
    Cursor1 = cr.Query(Uri1, Proj, ""Null"")
    Error:
    This is the log generated by cc_result:
    Any ideas on how to change GetPathFromContentResult?
     
  12. lymey

    lymey Active Member Licensed User

    I did some poking around and found this: http://stackoverflow.com/questions/...i-android-kitkat-new-storage-access-framework
    One of the suggestions to obtain the file name from Google Drive was to read '_display_name' from the cursor. So I altered the code:

    Code:
    Sub GetPathFromContentResult(UriString As StringAs String
       
    If UriString.StartsWith("/"Then Return UriString 'If the user used a file manager to choose the image
       Dim Cursor1 As Cursor
       
    Dim Uri1 As Uri
       
    'Dim Proj() As String = Array As String("_data")
       Dim Proj() As String = Array As String("_display_name")
      
       
    Dim cr As ContentResolver
       cr.Initialize(
    "")
       
    If UriString.StartsWith("content://com.android.providers.media.documents"Then
         
    Dim i As Int = UriString.IndexOf("%3A")
         
    Dim id As String = UriString.SubString(i + 3)
         Uri1.Parse(
    "content://media/external/images/media")
         Cursor1 = cr.Query(Uri1, Proj, 
    "_id = ?"Array As String(id), "")
       
    Else
         Uri1.Parse(UriString)
         Cursor1 = cr.Query(Uri1, Proj, 
    ""Null"")
       
    End If
       Cursor1.Position = 
    0
       
    Dim res As String
       
    'res = Cursor1.GetString("_data")
       res = Cursor1.GetString("_display_name")
       Cursor1.Close
       
    Return res
    End Sub
    This now returns the correct file name when using the chooser to select a Google Drive file.
     
    andyr00d and DonManfred like this.
  13. mohsen nasrabady

    mohsen nasrabady Active Member Licensed User

    some times the sub
    cs_Result (Success As Boolean, Dir As String, FileName As String)
    return nothing and log is
    onActivityResult: IOnActivityResult was released
    what's the problem?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Make sure that ContentChooser is a process global variable.
     
  15. mohsen nasrabady

    mohsen nasrabady Active Member Licensed User

    thnx the problem solved
     
  16. little3399

    little3399 Active Member Licensed User

    Hi, Erel
    Does the 1.zip sample can be only show PhotoLibrary and not include the Files Manager ? TKs!
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    What is 1.zip?
     
  18. little3399

    little3399 Active Member Licensed User

    Hi,

    maybe was this

    upload_2015-4-14_13-59-48.png
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    It is better to post a link next time.

    ContentChooser creates an intent with the given mime and sends it to the OS. Any application that is registered for this mime type will be returned.
     
  20. little3399

    little3399 Active Member Licensed User

    Ok, Tks Erel
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice