How can I lock files in android using java code? - java

I want to lock files that is on the sdcard of android.
I need to lock them so no one except my software can delete copy move or send them over bluetooth or any other way.
How can I do it?

There is no way to lock a file in Android; even if there was, the files would only be locked when your application was running which wouldn't stop other apps accessing the file when the user hadn't started your app.
SD Cards are usually formatted with FAT (or FAT32) and this is the case with Android. FAT offers very limited file permissions so you can't restrict access to the files this way. Again, even if you could do something like this, this wouldn't offer any protection if the user took the SD Card out of their phone and put it their PC.
If you want to keep your application's files private you need to put them in the phone's internal storage in the area reserved for you application. Each application runs with a different User ID and each application's data directory is protected using UNIX-style file permissions which prevents other applications from reading them. So while this will stop other applications having access to your files it wouldn't stop a reasonably determined user with root access to their phone copying them off.
Have a look at the Context.openFileInput(), Content.openFileOutput() and Context.getFilesDir() methods for details on how to store files in the phone's internal storage.

You cannot do this on any platform (let alone Android) using a regular SD card. Sorry!

I think you want to encrypt the file. While the file can still be moved around, it will be useless to anything but your application. People will be able to copy, move, delete but the contents won't be compromised.

Related

Copy files from Android's Gdx.files.local() folder to developing machine

LibGDX: I have game-data files created at runtime for debugging purposes (a replay system). The files are stored in Gdx.files.local() whether on desktop or android. Now I want to copy the ones that were created on android so I can debug them on desktop.
Running Gdx.files.getLocalStoragePath() on android returns /data/user/0/com.[my domain].[my app]/files/. Using Windows Explorer I do not see my app's folder in /data/user/0, or anything else for that matter. So private storage indeed it is, but how nonetheless can I get hold of these files, without root if possible?
The solution may be manual copy or in code, whatever works. Just to note, the file names are unknown without seeing the files first. They were named using current date-time. The extension is known, though.
EDIT
I discovered Android Studio's Device File Explorer. I connected my device, tried to see inside my app folder and app data folder, both showing 'Operation not permitted'.
So to put the question in simpler words, is rooting the phone really the only way for developers to see the files created by their apps?
Or is there a magical gate somewhere known only to select few zen masters who guard the secret through the generations? Because I sure ain't finding it.
I'm not sure that there is a way to write changes in the system file without having root access but you can read them. According to this Blog, You can access those files and put it in your desktop. You can download RAR app from googleplay.

Protect and unprotect file to avoid accidental elimination in Android by bugged cleaning apps

In my Android App I save some files with some data file using
FileOutputStream savedList = new FileOutputStream(Path);
in a folder named myApp located in the SD storage
Unfortunately I have noticed that some cleaner Apps, not well implemented, also very popular (e.g. CleanMaster) wrongly remove the files every time the user perform a temp\trash file cleaning causing problems.
Is there a way to protect (and unprotect for writing) the file programmatically to avoid this?
How to change file permissions?
Since aren't used the file extensions to recognize the file format, how could I change the metadata of the file that are used to determine the file format so that these file are see as documents by these apps? I suppose that the scan of these Cleaners use some strategy based on Linux file format recognition and remove all object files.
Android allows to have private directory on SD card for the app.
you can get the path for private directory for your app as follows.
File myDir = getExternalFilesDir(null);
The null parameter indicates that you are going to store any type of files in the directory
myDir.mkdirs();
Log.d("info", myDir.getPath());
These files are internal to the applications, and not typically visible to the user as media.
This is like getFilesDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:
Shared storage may not always be available, since removable media can be ejected by the user. Media state can be checked using getExternalStorageState(File).
There is no security enforced with these files. For example, any application holding WRITE_EXTERNAL_STORAGE can write to these files.
This solution worked for me as cleaning apps on devices don’t clean these folders considering them as private folders for the respective apps.
Checkout following link from android docs. Context.getExternalFilesDir(java.lang.String)
Write it to your private internal drive, so they don't have permission to touch it. SD cards are just FAT32 drives, so they don't support file permissions or access lists.
On 4.4 phones you may be ok, as Google basically prevents any writes to the SD card outside of a private directory. Cleaner type apps won't work on it at all, for better or worse.
First, you should read the first answer of this question. The thing to remember :
No, Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be "external storage". On some devices, this is removable media, like an SD card. On some devices, this is a portion of on-device flash.
The SD card is a vague notion, it's quite impossible to be 100% sure you are writting on the SD card.
That thing said, you should use the Android API to write your file on the private directory of the app, located in /path/to/external/storage/whatever/it/is/on/the/device/Android/data/com.package.yourapp/files
Use getExternalFilesDir to get the above File and write your file on the private directory of your app, this way, no one will be able to delete it.

Determine whether to use internal or external android app

Ive been reading up on where to store my files, and I am still not sure what route I should take. I am writing an application that will store images and pdf's (totaling 300MB or so). I would like the app installed on the internal storage, and then allow the user to store the pdfs and some images on there choice of storage. Is it possible to allow the user to choose which storage they use for those files only even I have this in my manifest:
android:installLocation="internalOnly"
Essentially I would like to have the user choose this when the applications start.
If it is, would I just need to have a flag in application that if the user chose external and if the external storage was available, had enough space, then save files to it. Every time the user opened the app I would check to make sure the files are available. If the users chose internal it would use openFileOutput. Does this all seem right, is this possible?
Or should I just consider making it completely internal only, because without these PDF's and images the app is not usable.
It sounds like you can set the installLocation as following to accomplish exactly what you want:
android:installLocation="preferExternal"
From the documentation:
If you declare "preferExternal", you request that your application be
installed on the external storage, but the system does not guarantee
that your application will be installed on the external storage. If
the external storage is full, the system will install it on the
internal storage. The user can also move your application between the
two locations.
If you set it to internalOnly, the user cannot move the application from internal storage to external storage, so above solution is probably your best option.

File operations doesn't work sometimes

Something very strange is happenning in my app. I am creating 2 folders in the SD card if they not exist, and downloading some images from a URL if they not exist in the SD card.
Sometimes, when I run the application, the program checks if the folders exists and also the images, as they are exists, it continues and there's nothing wrong.
But often when I run the application, it alerts me that the folders not exists and that it didn't even managed to create those folders, and afterwards I'm getting a "File not found exception" when it tries to download the images who already exists in my SD card.
P.S: I tried it with 2 devices and the same happens, sometimes works, sometimes doesn't.
What could be the problem here?
Actually before accessing files from external storage you have to check whether External storage is present (Available) or not on device (It possible if device's sdcard is mounted on System). For this You have to check the sate of External Storage as Mounted or Not.
Look at this Link for more info..
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
Now solution:
If you find external storage is not available then you can store your files at Internal Storage on temporary basis (Then you can move these files to External Storage when it present).
So My link also help you in how to access Internal Storage in Android.

Changing directory permissions

I need to get write permissions to the lib directory of my application (i.e. /data/data/com.my.app/lib/) and store a file there, and I need to do it from my application. Is it possible? and if so, how?
EDIT
By my application I mean using Java. /data/data/com.my.app/lib/ is the path to the lib directory of the same application.
EDIT
Unfortunately, as LeffelMania said, I couldn't find any convenient way to do it. Fortunately, we solved this problem in a better way - we convinced the developers of the library to make it more dynamic :). Thanks again LeffelMania.
It is not possible to directly write into another app's file space. Your app has write access to its own space and external storage (SD card) with permission.
However, I can imagine a couple ways to do this, depending on what you're trying to do, specifically.
1) Write the file to the SD card from one app. Have the other check if it's there and read from it when it opens. Make sure to write it to your own proprietary folder - don't clog the user's storage space. If applicable to your situation, delete the file and your folders on the SD card from the other app once you've read the file.
2) Register a custom BroadcastReceiver in the recipient app, and stick the data you need to transfer inside of an Intent that you will send from the other app. You should use your own action String so that no other application in the system attempts to use your Broadcast.

Categories

Resources