Android Question Error opening file with ContentChooser

Roldanx

New Member
Erro: (Exception) java.lang.Exception: java.io.FileNotFoundException: /storage/emulated/0/PDFTeste.pdf: open failed: EPERM (Operation not permitted)



Error on Line 17:
    Dim CC As ContentChooser
    CC.Initialize("cc")
    CC.Show("application/pdf", "Choose pdf file")
    Wait For CC_Result(Success As Boolean, Dir As String, FileName As String)
  
    If Success = True Then
        Log(Dir)
        Log(FileName)
    Else
        Msgbox("Não foi possível abrir o arquivo, por favor escolha o arquivo PDF e clique em cima dele.", "Arquivo não encontrado")
        Return
    End If

    Try
       File.Copy(Dir, FileName, File.DirRootExternal, "PDFTeste.pdf")
    Catch
        Log(LastException)
    End Try
  
    Dim filepdf As String = File.DirRootExternal & "/PDFTeste.pdf"
    Dim filetxt As String = File.DirRootExternal & "/PDFTeste.txt"
  
    Dim pdf As PdftToText 
    pdf.Initialize("pdf")
    pdf.ParsePdf(filepdf, filetxt)
    Dim wString As String
  
    Try
        wString = File.ReadString(File.DirRootExternal, "PDFTeste.txt")
    Catch
        Log(LastException)
    End Try
  
    Msgbox(wString, "texto do PDF")
 

Roldanx

New Member
Error on Line 17:
    Dim CC As ContentChooser
    CC.Initialize("cc")
    CC.Show("application/pdf", "Choose pdf file")
    Wait For CC_Result(Success As Boolean, Dir As String, FileName As String)
  
    If Success = True Then
        Log(Dir)
        Log(FileName)
    Else
        Msgbox("Não foi possível abrir o arquivo, por favor escolha o arquivo PDF e clique em cima dele.", "Arquivo não encontrado")
        Return
    End If

    Try
       File.Copy(Dir, FileName, File.DirRootExternal, "PDFTeste.pdf")
    Catch
        Log(LastException)
    End Try
  
    Dim filepdf As String = File.DirRootExternal & "/PDFTeste.pdf"
    Dim filetxt As String = File.DirRootExternal & "/PDFTeste.txt"
  
    Dim pdf As PdftToText 
    pdf.Initialize("pdf")
    pdf.ParsePdf(filepdf, filetxt)
    Dim wString As String
  
    Try
        wString = File.ReadString(File.DirRootExternal, "PDFTeste.txt")
    Catch
        Log(LastException)
    End Try
  
    Msgbox(wString, "texto do PDF")
O error : (Exception) java.lang.Exception: java.io.FileNotFoundException: /storage/emulated/0/PDFTeste.pdf: open failed: EPERM (Operation not permitted)
 
Upvote 0

Roldanx

New Member
Sempre que você vê um código com File.DirRootExternal ou Msgbox você sabe imediatamente que ele está quebrado. Use a pesquisa do fórum e você encontrará muitos tópicos sobre esses dois.

Whenever you see code with File.DirRootExternal or Msgbox you immediately know that it is broken. Use the forum search and you will find many many threads about these two.
my problem is only with the line:
File.Copy(Dir, FileName, File.DirRootExternal, "PDFtest.pdf")
which does not copy the file, which gives the error:
The error: (Exception) java.lang.Exception: java.io.FileNotFoundException: /storage/emulated/0/PDFTeste.pdf: Failed to open: EPERM (Operation not allowed)

it is not an authorization problem, the problem is that ContentChooser returns the Dir and when I try to copy the file does not exist, am I being clear?
 
Upvote 0

zed

Active Member
Licensed User
You cannot use File.DirRootExternal.
You have to use rp.GetSafeDirDefaultExternal("subfolder")

Replace msgbox with
MsgboxAsync("Error msgbox","")
 
Upvote 0

zed

Active Member
Licensed User
I use rp.GetSafeDirDefaultExternal("subfolder") in development mode.
It allows me to check if all the files are well copied, which is not possible with xui.DefaultFolder then,
I change rp.GetSafeDirDefaultExternal("subfolder") for xui.DefaultFolder.
Is this a bad thing?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It allows me to check if all the files are well copied, which is not possible with xui.DefaultFolder then
Why not? Don't confuse File.DirInternal=XUI.DefaultFolder with File.DirAssets.

Anyway, there is nothing bad with using GetSafeDirDefaultExternal, it is just not needed any more and doesn't provide any benefit.
 
Upvote 0
Top