Italian [B4A] Registratore (errore nella registrazione)

Aldo's

Active Member
Licensed User
Sto facendo un piccolo registratore per il mio cell.
Quando provo a registrare mi da questo errore:

Error occurred on line: 163 (Registratore)
java.io.FileNotFoundException: /storage/emulated/0/2.wav: open failed: EPERM (Operation not permitted)
at libcore.io.IoBridge.open(IoBridge.java:574)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:289)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.Initialize2(RandomAccessFile.java:103)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7558)
at android.view.View.performClickInternal(View.java:7534)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:29661)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:240)
at android.os.Looper.loop(Looper.java:351)
at android.app.ActivityThread.main(ActivityThread.java:8377)
at java.lang.reflect.Method.invoke(Native Method)

Per comodità, spero vostra, metto lo zip con tutto il progetto.
Spero qualcuno riesca a dirmi dove sbaglio.
Grazie
 

Attachments

  • Project.zip
    21.9 KB · Views: 103

Aldo's

Active Member
Licensed User
In pratica mi da' errore alla 4^ riga di questa sub
B4X:
Sub StartWaveFile(Dir As String, FileName As String, SampleRate As Int, Mono As Boolean, BitsPerSample As Int) As OutputStream
    File.Delete(Dir, FileName)
    Dim raf As RandomAccessFile
    raf.Initialize2(Dir, FileName, False, True)
    raf.WriteBytes("RIFF".GetBytes("ASCII"), 0, 4, raf.CurrentPosition)
    raf.CurrentPosition = 8 'skip 4 bytes for the size
    raf.WriteBytes("WAVE".GetBytes("ASCII"),0, 4, raf.CurrentPosition)
    raf.WriteBytes("fmt ".GetBytes("ASCII"),0, 4, raf.CurrentPosition)
    raf.WriteInt(16, raf.CurrentPosition)
    raf.WriteShort(1, raf.CurrentPosition)
    Dim numberOfChannels As Int
    If Mono Then numberOfChannels = 1 Else numberOfChannels = 2
    raf.WriteShort(numberOfChannels, raf.CurrentPosition)
    raf.WriteInt(SampleRate, raf.CurrentPosition)
    raf.WriteInt(SampleRate * numberOfChannels * BitsPerSample / 8, raf.CurrentPosition)
    raf.WriteShort(numberOfChannels * BitsPerSample / 8, raf.CurrentPosition)
    raf.WriteShort(BitsPerSample, raf.CurrentPosition)
    raf.WriteBytes("data".GetBytes("ASCII"),0, 4, raf.CurrentPosition)
    raf.WriteInt(0, raf.CurrentPosition)
    raf.Close
    Return File.OpenOutput(Dir, FileName, True)
End Sub

che richiamo in questo modo:
B4X:
    output = StartWaveFile(File.DirRootExternal, sFileRec, mSampleRate, mMono, mBitRate)

L'errore è:
Error occurred on line: 159 (Registratore)
java.io.FileNotFoundException: /storage/emulated/0/1.wav: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:574)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:289)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.Initialize2(RandomAccessFile.java:103)
at b4a.example.registratore._startwavefile(registratore.java:461)
at b4a.example.registratore$ResumableSub_bttRec_Click.resume(registratore.java:408)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:150)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.keywords.Common$15.run(Common.java:1804)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:240)

Grazie
 

Aldo's

Active Member
Licensed User
Allora dopo un po' di prove ho visto che il problema è File.DirRootExternal, mettendo File.DirInternal mi sembra dia meno problemi.
Come faccio a metterli in una sottodirectory di File.DirInternal?
 

Sagenut

Expert
Licensed User
Longtime User
Allora dopo un po' di prove ho visto che il problema è File.DirRootExternal, mettendo File.DirInternal mi sembra dia meno problemi.
Sì il problema è proprio quello
It will not work with targetSdkVersion=30. Avoid using File.DirRootExternal. Either use File.DirInternal or RuntimePermissions.GetSafeDirDefaultExternal.
Se però l'app è solo ad uso personale puoi mettere il TargetSDK a 26 ed evitare il problema.
 

Aldo's

Active Member
Licensed User
Difatti l'errore non me lo da più. In compenso non registra.
 

Aldo's

Active Member
Licensed User
Creala.
B4X:
File.MakeDir(File.DirInternal, "MyNewFolder")
Prima di creare la directory non è il caso di verificare che ci sia?
Come si fa?
 

LucaMs

Expert
Licensed User
Longtime User
Prima di creare la directory non è il caso di verificare che ci sia?
Come si fa?
B4X:
Private Sub WriteText
    Dim MyFilesFolder As String = "MyFilesFolder"
    If Not(File.Exists(File.DirInternal, "/" & MyFilesFolder)) Then
        File.MakeDir(File.DirInternal, "/" & MyFilesFolder)
    End If

    Dim FullPath As String
    FullPath = File.Combine(File.DirInternal, "/" & MyFilesFolder)
    
    File.WriteString(FullPath, "MyFile.txt", "Mio testo")
    
    Dim Text As String = File.ReadString(FullPath, "MyFile.txt")
    Log(Text)
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…