Android Question Set a file as ReadOnly by code

LucaMs

Expert
Licensed User
Longtime User
I tried this code:
B4X:
Sub SetReadOnly(Dir As String, FileName As String) 'ignore
    Dim r As Reflector
    r.Target = r.CreateObject2("java.io.File", Array As String(Dir, FileName), _
        Array As String("java.lang.String", "java.lang.String"))
    r.RunMethod("setReadOnly")
End Sub


but it does not work.


There is also this method:
boolean
setWritable(boolean writable, boolean ownerOnly)
but >= API 9.

The description:
Manipulates the write permissions for the abstract path designated by this file.

What means: "abstract path designated..."?
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
What OS is the Device running? Which version...



public boolean setWritable (boolean writable, boolean ownerOnly)
Added in API level 9
Manipulates the write permissions for the abstract path designated by this file.

Parameters
writable To allow write permission if true, otherwise disallow
ownerOnly To manipulate write permission only for owner if true, otherwise for everyone. The manipulation will apply to everyone regardless of this value if the underlying system does not distinguish owner and other users.
Returns
  • true if and only if the operation succeeded. If the user does not have permission to change the access permissions of this abstract pathname the operation will fail.


Starting on Android 4.idontknow the rights are limited to apps so i suppose you get an false as result when trying to use setWriteable on such devices (limited access-rights)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I tried setReadOnly, which requires API 1 (I wrote setWritable just for an alternative method).
Also, I should have the permission, given that I have created the file.

I have tried both DirRootExternal and DirDefaultExternal.

But I found this answer (web) about setWritable:
The sdcard uses a FAT-type filesystem which does not support file ownership.

It is possible at implementation level for FAT to support a read-only bit, but that would be of limited value since anything else with sd card write permission would be free to change the bit. Given the limited value, it's an open question if it's even possible to set that on android.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User

>Posted on August 23, 2010
>File file = new File("c:/file.txt");>

Seems that this post is facing a windows-java-code-snippet... And this post is four years old.
When did android 4 came out... End of 2011 i supose....

The restrictions to apps in android came with android 4.something... I believe it was 4.4 but not sure.

That´s why i asking which android version the device is running which you tried
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Well, I created a file, then I called the routine to make the file readonly and then I tried to delete the file (in a Try-Catch).

Unfortunately it was deleted in each test.
 

Attachments

  • lm setReadOnly.zip
    11.5 KB · Views: 305
Upvote 0
Top