Android, writing on specific sectors - java

I wonder if there's a possibility to create an android app in java which will be installed on specific memory sectors of phone(not the sd card). So those sectors can be accessed from other app and it can be completely erased(couple times write/erase so it won't be recovarable). Do you have any ideas?
Best Regards,
Martin

If I understand you correctly then I think what you want is not possible.
All downloaded apps are instaleld to /data/app/ or SD card. There is no way to put them anywhere else under normal circumstances.
What you can do however is make MODE_WORLD_ WRITABLE files from within your application. If you do that then other applications will be able to see and manipulate those specific files.

Related

How to access files under /sdcard/Download in Android 11 without SAF? [duplicate]

I'm still searching, but maybe anyone faced with the same issue.
I'm maintaining an app. This app stores some data in two places (lets say on sdcard/example and external_memory/example). Before the migration to the newest Android Api everything worked fine using ExternalStorage.getAllExternalStoragePaths(). Now I'm still able to get both paths, but the sdcard/example path is not readable and not writable..
I read about the SAF, but in my case that is not a solution - the files stored in both directories are parsed using a native library (C++) - I need path to it like /storage/sdcard/example/test.xml. Otherwise the library is not able to read this file. Unfortunately I cannot change the library because of missing sources.
I would like to ask three questions:
Is there any other way to ask access to directory on SD Card and use this directory like the old way?
Maybe I can say Android - "hey, I want have Example directory on SD Card and maintain it"?
Or maybe I can use SAF without ContentResolver?
Moving files to /storage/sdcard/Android/data/com.example/files/ is not acceptable by stakeholders.
Is there any other way to ask access to directory on SD Card and use this directory like the old way?
No, short of building a custom ROM or rooting the device.
Maybe I can say Android - "hey, I want have Example directory on SD Card and maintain it"?
No. The closest thing is getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs(). If those return 2+ items, the second and subsequent ones will be on removable media, and you can read and write to those paths. However, you do not control what the paths are.
Or maybe I can use SAF without ContentResolver?
No, insofar as you cannot get a filesystem path.

Java, Codename One: Exporting and -most importantly- importing files between users

Here the problem: my app will generate some files, and I want to give to the users the opportunity to exchange these files between them.
This requires 3 steps:
Saving the data: easily done in Storage implementing the
functions required by the Externalizable class;
Sharing the data: done (probably, right now it's impossible to check if the
result is correct because the missing step 3) with the sharing
methods offered by the framework, as soon as I understood I needed
to use as mimetype "application/octect-stream";
importing the downloaded data (shared by another user): this one I can't manage to
find a way to make it work. Loading the files from the app's Storage
is easy, but accessing to the folders out of the app's Storage is
something I can't manage to do.
I used FileSystemStorage in the hope of gaining access at least to the Download folder that (mostly) every phone has, but apparently I can't manage to accomplish the task
Using the FileSystemStorage on Android, for example, I have access to
/storage/emulated/0
/storage/emulated/legacy
file:///system
The first two being related to the Storage of the app.
Acceding to file:/// I obtain a long list of folders, a partial list including
media
logs
sdcard
Removable
...
root
...
But when I try to access some of these, they all appear to be empty. Either I make some mistake or the app can't see their content.
So I wonder if there is a way to accomplish the task, namely to have access to the files (probably in the Download folder) the user has downloaded, to import them.
Phone apps live in isolation within the phone. Android literally creates a separate Linux user for every app so they don't step on each other and damage the phone. iOS does similar tricks.
As a result apps can't just write a file to downloads and can't just list all the files there. This would violate the users privacy. So the native API to share a file is usually separate from the files API. We do have a share API in the CN class which lets you share images etc. to an arbitrary OS app. See isNativeShareSupported and share.
Ok, maybe I found a solution for reading the files from the Download folder in an extension of CodenameOne called FileChooser.
According to this blog post it should give access to, between the others, the Download folder (at least in Android).
I'm going to try it and, when everything is ready and tested, edit this reply to say how it worked out for me.

is this the best storage option for me?

I want to save downloaded images from a server. I want these images to be accessible only from within the application itself. I don't want the images accessible from anywhere else, i.e. someone can just delete/modify it like if it was on the SD card or from another different application. I'm thinking it would be best if I were to use internal storage, as it is private to my app.
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
are there better options out there?
Internal storage is the best option if you want to keep your files private.
However, on most devices, internal storage is very limited and the users might uninstall your app if you use too much of it (images can be pretty big).
You should look into using the external storage to save images, and possibly encrypting them if you really want to keep them private.
As already mentioned by Raghav this would be a good option, and as said the internal storage is very limited (mostly on older devices), however if you are on a rooted device it will be possible to delete your files anyway. Take into account the limited storage, as people is also making "swap partitions" on their SD cards because internal storage is very limited.
This is is the simplest solution. The other typical solution is to store the image as a blob in the application's database. This will make it completely inaccessable outside of your application. You can do it using the method shown here:
How to store and retrieve a byte array (image data) to and from a SQLite database?
The draw back to this is the relatively small memory space you will be given for your DB (<60MB), so this only works if you have a small number of images.

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.

How can I lock files in android using java code?

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.

Categories

Resources