Android Question Copy file

elitevenkat

Active Member
Licensed User
Longtime User
while executing File.Copy("",dm,File.DirInternal,"mdata.xlsx") the following error is thrown can anyone help please

java.io.FileNotFoundException: /storage/emulated/0/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Documents/grid_tbl_pos_items.xls: open failed: EACCES (Permission denied)
manifest entry
B4X:
AddManifestText(<uses-permission
   android:name="android.permission.WRITE_EXTERNAL_STORAGE"
   android:maxSdkVersion="30" />
)

AddManifestText(<uses-permission
   android:name="android.permission.WRITE_INTERNAL_STORAGE"
   android:maxSdkVersion="30" />
)
B4X:
dim uri10 as string="content://0@media/external/file/45"   
Dim dm As String = GetPathFromContentResult(uri10)
     Log(dm)  ' return  --- '    /storage/emulated/0/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Documents/grid_tbl_pos_items.xls
    File.Copy("",dm,File.DirInternal,"mdata.xlsx")
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("")
    Log(UriString)
    'content://com.android.providers.media.documents/document/image%3A19
    'content://com.android.providers.media.documents/document/image%3A19
    
    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/images")
        Uri1.Parse("content://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
 

josejad

Expert
Licensed User
Longtime User
Hi:

Take a look to this post

 
Upvote 0

elitevenkat

Active Member
Licensed User
Longtime User
Hi:
i went thru the link you had provided.
my problem is accessing any data from other applications and nor from any external devices. i am trying to copy a file from document folder by selecting the same usng content chooser to the dirinternal folder. but getting the error java.io.FileNotFoundException: /storage/emulated/0/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Documents/grid_tbl_pos_items.xls: open failed: EACCES (Permission denied)

any working solutions please
 
Upvote 0

elitevenkat

Active Member
Licensed User
Longtime User
Every bit of code in the first post is wrong. The permission declaration is wrong. GetPathFromContentResult is wrong.
it is obvious that my code is wrong and hence not yielding the desired result. Can anyone help me with a sample code please.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Arbitrary access to secondary storage is more difficult when targeting SDK above 28. Some available methods are here.
 
Upvote 0

elitevenkat

Active Member
Licensed User
Longtime User
Thank you.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…