File.Delete not working

bgsoft

Well-Known Member
Licensed User
Hello

In a panel'm creating images at runtime.
If I press on the image, leaving a message if you want to delete.
I want to delete with File.Delete but it does not work, it does not erase and returns me false.
I checked the directory and the name of the image and is correct.
The image to remove no matter who is in the internal memory, or on the SDCard ExSdCard, not delete.
I tried to remove the image (RemoveView), load another picture (b.Bitmap = LoadBitmapSample (Directory, NombreFiles.Get (0), 15% x 15%)), wait 200 milliseconds, but none of this has worked.
So do not do wrong, or if you have to add something in the Manifest.

Anyone can help me

thanks

Jesus

:sign0013: For my bad English

B4X:
Sub Button_LongClick

  Dim NombreImagen As String
  Dim id As Int
  Dim Resultado As Boolean

  Dim b As ImageView
  b = Sender
  NombreImagen =b.Tag
   
   
  id=Msgbox2("Quiere eliminar la imagen:" & CRLF & NombreImagen,"Eliminar", "Aceptar","Cancelar","",I_BGS.Bitmap)

  If id = DialogResponse.Positive Then 
    ' borrar imagen
      'b.RemoveView
      Dim Bitmap1 As Bitmap
      Bitmap1.Initialize3(ImagenVacia.Bitmap)
      'b.Bitmap=LoadBitmapSample(Directorio,NombreFiles.Get(0),15%x,15%y)
      DoEvents
      Sleep(200)
      Resultado=File.Delete(Directorio,NombreImagen)
      If Resultado = True Then
        ToastMessageShow("Imagen eliminada" & CRLF & NombreImagen,True)  
        CargarImagenes
        LeerImagenes
      Else
        ToastMessageShow("No se pudo eliminar la imagen:" & CRLF & NombreImagen,True)  
      End If
   End If


   
End Sub
 

bgsoft

Well-Known Member
Licensed User
The directory I take from FilePath, do it with a FileDialog (library Dialogs v. 2.7).
It is difficult to to be wrong in doing that. Also, I see a log and is correct.
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
I forgot. That picture I see it correct. If the directory or file name was wrong, would not see the image in a ImageView
 
Upvote 0

stevel05

Expert
Licensed User
I've tried this:

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim IV As ImageView
   Dim BMP As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   
   
   IV.Initialize("IV")
   Activity.AddView(IV,0,0,100%x,100%y)
   
   File.Copy(File.DirAssets,"logo.png",File.DirRootExternal,"logo.png")
   IV.Bitmap=LoadBitmap(File.DirRootExternal,"logo.png")
   
   Log(File.Delete(File.DirRootExternal,"logo.png"))                         'Returns True
   Log(File.Exists(File.DirRootExternal,"logo.png"))                          'Returns False
End Sub

To emulate what you are doing and it works as expected. The file is deleted while it is still displayed, so no locking issue there.

Can you try puting:

B4X:
Log(File.Exists(Directorio,NombreImagen))

Immediately before File.Delete(.......) and see what you get in the log.

Does NombreImagen include a file extension?
 
Last edited:
Upvote 0

bgsoft

Well-Known Member
Licensed User
Thanks for helping.

I placed the lines of programs before and after clearing, and I mention results.
And yes, the file has extension.

thanks

I try your code tomorrow

B4X:
Log(File.Exists(Directorio,NombreImagen))  ' true
Resultado=File.Delete(Directorio,NombreImagen)
Log(File.Exists(Directorio,NombreImagen))  ' true
Log("Directorio:" & Directorio & "-NombreImagen:" & NombreImagen) ' Directorio:/storage/extSdCard-NombreImagen:MotoFairing.jpg
Log("Resultado-" & Resultado & "-") ' false
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Hello stevel05

You're going to laugh :D.

I cleaned the code above and put only the portion of the file deleted. And still not working.
To not create a new program folder, and image. I copied the directory File.DirAssets, an image called logo.png, so test your code.
I've put your code at the end of mine, I have incredibly deletes my image, and your code also works.
If I take your code not deletes my image, I will continue to investigate why this is not normal.

Note: Do not make the obvious joke that let your code :D

regards

Jesus

B4X:
Sub Button_LongClick

  Dim NombreImagen As String
  Dim id As Int
  Dim Resultado As Boolean

  Dim b As ImageView
  b = Sender
  NombreImagen =b.Tag
   
   
  id=Msgbox2("Quiere eliminar la imagen:" & CRLF & NombreImagen,"Eliminar", "Aceptar","Cancelar","",I_BGS.Bitmap)

  If id = DialogResponse.Positive Then 
    ' borrar imagen
    Resultado=File.Delete(Directorio,NombreImagen) ' return True :-)
    If Resultado = True Then
      ToastMessageShow("Imagen eliminada" & CRLF & NombreImagen,True)  
      LeerImagenes
      CargarImagenes
    Else
      ToastMessageShow("No se pudo eliminar la imagen:" & CRLF & NombreImagen,True)  
      End If
   End If

''''''''''''''''' TEST ''''''''''''''''''''''''''''

    IV.Initialize("IV")
    Activity.AddView(IV,0,0,100%x,100%y)
    
    File.Copy(File.DirAssets,"logo.png",File.DirRootExternal,"logo.png")
    IV.Bitmap=LoadBitmap(File.DirRootExternal,"logo.png")
    
    Log(File.Delete(File.DirRootExternal,"logo.png"))  ' return True
    Log(File.Exists(File.DirRootExternal,"logo.png"))   ' return false     

''''''''''''''''' TEST ''''''''''''''''''''''''''''


   
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Hmmm, That is strange. As you say need to keep looking.

I seem to remember somewhere about possible problems using Modal Dialogs with Listeners (Long_Click Listener) unless I imagined it, I'll have a quick search of the forum in a while, unless you find the problem first.
 
Upvote 0

stevel05

Expert
Licensed User
No, didn't find anything and testing it wouldn't fail either. I must have imagined it.
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Hello stevel05

Thanks for your interest.
After that test. I thought that was the problem Longclick. And create a "sub" to delete the file

B4X:
Sub Button_LongClick

  
 
  Dim NameFile As String
  Dim BotonImagen As ImageView

  BotonImagen = Sender
  NameFile =BotonImagen.Tag
   
  BorrarImagen(NameFile) ' call to delete the file


end sub

The result is that neither the file erased.

At the end of many tests removing and part of your code.
I found that leaving this line of code work (quite incredible)

B4X:
File.Copy(File.DirAssets,"logo.bak",File.DirRootExternal,"logo.png")

As I do not want this by copying a file. I say to copy a file that does not exist.
And the code is working well:


B4X:
Sub Button_LongClick
   
  Dim NameFile As String
  Dim BotonImagen As ImageView

  BotonImagen = Sender
  NameFile =BotonImagen.Tag
   
  BorrarImagen(NameFile) ' call to delete the file
   

   ' si no lo pongo no funciona el borrar aunque parezca increible
        ' If this line is not working the call to delete, and that the call is before, quite incredibly

   Try
     File.Copy(File.DirAssets,"logo.bak",File.DirRootExternal,"logo.png")
    Catch
     Log("error al copiar")
    End Try

   
End Sub

I think clearly that is an error B4A, anything should happen to compile this line makes it work.
I've tried to put other files related codes, but does not work.
For now I leave it like that, now I do not want to waste any time on this nonsense :BangHead:.

Thanks for your help.

Jesus
 
Upvote 0

stevel05

Expert
Licensed User
There must be something else in the code that is causing this. It's possible that it could cause errors elsewhere if you don't track it down.

Have you been able to try it on another device?

Can you create a small version of the program that you can post that has the error?
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Create a smaller version would give me a lot of work.
I used a program on the forum and I have added the code on the longclick. And it does not work. My program also uses the TouchImageView.
TouchImageView_v_2_00.zip -> http://www.basic4ppc.com/forum/addi...l-updates/15616-touchimageview.html#post88613

I added the code below

If you take away the cometary File.Copy, so if that eliminates :)

I hope you can find something

thanks

Jesus

B4X:
   Sub Activity_Create(FirstTime As Boolean)
    '....
   Bitmap1.Initialize( "sdcard", "LivetoRide.jpg")
   '.....

Sub TouchImageView1_LongClick(X As Int, Y As Int)
   'Log("LongClick X="&X&", Y="&Y)
   
   '   set the zoom/scale to 100%
   '   in this demo ScaleX and ScaleY will always be equal
   'TouchImageView1.PreScale(1/TouchImageView1.ScaleX, X, Y)
   
   Dim Resultado As Boolean
   Dim id As Int
   
   Log(File.Exists("sdcard", "LivetoRide.jpg"))
   
   id=Msgbox2("Delete imagen:" ,"Delete", "Yes","No","",Null)

   If id = DialogResponse.Positive Then 
                Resultado=File.Delete("sdcard", "LivetoRide.jpg")
      Log("Resultado " & Resultado)
   End If   
   
   Log(File.Exists("sdcard", "LivetoRide.jpg"))
   
   
   
   '*********** Remove the comment to delete :-)  **************
'   Try
'     File.Copy(File.DirAssets,"logo.bak",File.DirRootExternal,"logo.png")
'    Catch
'      'Log("error al copiar")
'    End Try

   
   
End Sub
 

Attachments

  • LivetoRide.jpg
    LivetoRide.jpg
    13.3 KB · Views: 131
Upvote 0

stevel05

Expert
Licensed User
OK, you are using the wrong 'Directory' address, where you have "SDCARD" it should be "File.DirRootExternal" assuming you want the root.

I have changed it in the attached project which works as expected, I changed the file name too to one I had.

Why it loads the file, and works with the extra line of code, I don't know. But try changing the Directory address and see.
 

Attachments

  • test.zip
    25 KB · Views: 139
Last edited:
Upvote 0

bgsoft

Well-Known Member
Licensed User
Hello stevel05

As I said Jack the Ripper: "go by parts" :D
Not quite true that putting "sdcard" is putting the wrong direction. Because when I put:

Log (File.Exists ("sdcard", "LivetoRide.jpg")) returns true

Put "sdcard" because it is the value that returned my FilePath using a FileDialog, and for example not want to create an entire call FileDialog, as I have in the program.
I used your example and erased :sign0098:
I put a log, and I returned the FileDialog same name by putting it in my string "sdcard".
To check if my program directory "sdcard" was the same as File.DirRootExternal, put a log and the names were the same.
The good thing was, I tried deleting and cleared well. And without putting the code:

B4X:
  File.Copy (File.DirAssets "logo.bak" File.DirRootExternal "logo.png")

So the conclusion is that by making a reference to "File.DirRootExternal", the program deletes, but if I comment this log, do not delete.

This is the code where I select a folder to see the file, and simply putting the end of the log, the program deletes.
Not much chance that before you know it going to do a File.Copy as directory "File.DirRootExternal" I also worked the program?

Now someone will have to think that if I put a log referencing "File.DirRootExternal" works.

Now, as I pass FileDialog (FilePath) directory "/mnt" to go to the root, there is a system variable to do the same?

Thanks for everything, and forgive my bad English

Jesus


B4X:
Sub BuscarDirectorio
   Dim id As Int
   Dim P As String
   Dim OldDirectorio As String
   Dim lf As List
   
   OldDirectorio = Directorio

   fd.FastScroll = True
   fd.FilePath = Directorio
   fd.FileFilter = ".jpg,.gif,.png"      
   'fd.ShowOnlyFolders = True
   fd.Show ("Seleccione carpeta", "Aceptar","Cancelar","",I_BGS.Bitmap) 

   id = fd.Response
   If id = -1 Then 
      P= fd.FilePath 
       lf = File.ListFiles(P)
       If lf.IsInitialized = False Then
         Msgbox("No se puede acceder a la carpeta","")
          Return
       End If
       If OldDirectorio = P Then
          Return
       End If    
      Directorio = P

      Log("File.DirRootExternal-" & File.DirRootExternal & "-")  ' If I remove this line does not work
       
   End If    

End Sub
 
Upvote 0
Top