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
You can use this code to get the full path (if such is available):
It depends on the following libraries: ContentResolver, SQL and Phone.B4X:Sub GetPathFromContentResult(UriString As String) As 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
content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D3
I have never tried this before, and can't find a good example. But this is what I did:You should be able to open an InputStream to this url.
Sub GetPathFromContentResult(UriString As String) As 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
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!java.io.FileNotFoundException: /(String) ContentDir/content:/com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D3: open failed: ENOENT (No such file or directory)
That's great I can get the file content - thank you.Try this:
B4X: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
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%x, 100%y))
End Sub
Sub GetPathFromContentResult(UriString As String) As 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
Cursor1 = cr.Query(Uri1, Proj, "", Null, "")
java.lang.IllegalArgumentException: Unknown column requested: _data
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
Success = true, Dir = ContentDir, FileName = content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D3
This is what happens using Google Drive with the ContentChooser with the following code:
Sub GetPathFromContentResult(UriString As String) As 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
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?