Can it possible to create password protected folder in Android? - android

Can I create password protected folder in Android?
File nf = new File("Folder");
nf.mkDire();

First, new File("Folder") has no meaning in Android. Always use some method to get at a root location, whether that is for internal storage or external storage.
Second, you cannot create a password-protected folder in Android at the OS level. You are welcome to store files in internal storage, in which case they are only accessible via your app, which can require a password (exception: people who root their devices can get at the files directly).

Related

Creating a txt file and viewing it in the File manger

I have created an android app that gets input from user through EditText and writes them to name.txt file in phone's internal storage. Is it possible to open the text file in phone's file manager? I tried to get the file path using getFilesDir().getAbsolutePath()+"/"+FILE_NAME. But couldn't locate the file in file manager.
You need to use External storage if you want another app like File Manager to access the file. Internal storage is only readable by your app.
In the comments you ask a valid question - "What if the phone doesnt have external storage...?". That is not really a concern today. See https://developer.android.com/training/data-storage/files:
Many devices now divide the permanent storage space into separate
"internal" and "external" partitions. So even without a removable
storage medium, these two storage spaces always exist...
==========
So change your above code to this:
getExternalFilesDir().getAbsolutePath()+"/"+FILE_NAME
getExternalFilesDir is a method from the android.content.Context class. So this call will work from your activity class which is a Context.
=============
Further supporting the choice of external storage is the following, also from https://developer.android.com/training/data-storage/files.
Internal storage is best when you want to be sure that neither the
user nor other apps can access your files.
External storage is the best place for files that don't require access
restrictions and for files that you want to share with other apps or
allow the user to access with a computer.
there is private storage for each app that can be accessed from the app itself and then public storage /sdcard/... that other app can access too (it needs to get Storage Permission from system)
this method will save a content in a file in private storage of app
public void saveFile(String fileName, String content) {
try {
FileOutputStream fOut = context.openFileOutput(fileName, Context.MODE_PRIVATE);
fOut.write((content).getBytes());
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Is it possible to list files from cache directory of another app?

As described in the documentation, internal storage files are private to application.
But, is there any way just to access list of for ex. cache directory files of another app?
Context context = createPackageContext(packageNameOfAnotherApp, 0);
File directory = context.getCacheDir();
directory.listFiles(); // this will return null, as do list()
No, for security reasons obviously.
You can only access data of other apps it they have written it to a publicly readable directory, or using a ContentProvider.
Unless device is rooted and your application has elevated privileges you cannot access private data of other applications directly.

Logger inside android based on database

I am working on logs inside android, I thought two ways for storing logs, and one is on external directory as a text file or a log file while other is to store in database. I found database method more useful in my case. My question is if I UN-install and reinstall the app will the database will be affected? In case of yes what should I do? I cannot place the logs online. How to take the backup or safe that database so it won’t be affected in case of UN- installation.
My question is if I uninstall and reinstall the app will the database will be affected?
Yes, your database will by default be stored in the application's data directory, which is deleted along with your application on uninstallation.
You can instead write a file of a filetype of your choosing (whether that's a simple text file, or a database file) to the external storage. You can obtain the directory path using:
Environment.getExternalStoragePublicDirectory(type) for obtaining a directory path of files of a specific type, such as images or videos;
Environment.getExternalStorageDirectory() for obtaining the primary external storage directory, under which you could create a new path.
I would nevertheless discourage you from doing this, because it would require your users to manually dispose of any files after uninstalling your application. Perhaps you should reconsider the justification of choosing for this solution.
If you uninstall the app, then the database (and all other data stored in the apps private storage for that matter) will be gone.
You could store your logs in the public external storage, but this will expose your logs to other applications as well as the user.
One possible approach could be to use application private storage for your 'live' logs, and make periodic backups to the public storage. In case of a new installation, you can check your designated backup location and attempt to restore previous logs from the backup.

Secure path in Android FileSystem?

I want to save my Android Application images and their details files in some secure path (so that any Android Application user can't access that files or you can say that it should be hidden to the User).
Can Anyone Help me...
If the device is rooted, the user will have an access to any file. If not, you can use internal storage.
Accessing the chache or files folder is done using the Context. the context can be your activity (activity extends context), and if you want to access the directory from a class which is not your activity / service you will need to pass a reference of the activity to this class.
Short example:
File chacheDir = myActivity.getCacheDir();
Create a folder with start with "."filename(ignore quotes) this will be always hidden to the user since android is linux based file system. you can store all your image into that folder.

Storing Files on Android that don't get deleted on update or uninstall

I want to store some data to a File (not database) on the Android device. Currently I'm using Context.getFileDir() as the parent directory. However, when you update the app that directory gets deleted. Is there a directory I can use from Context that won't get wacked when the user goes to update the application? Does it have to be the SD Card since that's not always generally available on all phones?
However, when you update the app that directly gets deleted.
No, it doesn't.
Is there a directory I can use from Context that won't get wacked when the user goes to update the application?
No files ever "get wacked when the user goes to update the application".
All files in the on-board flash will "get wacked when the user" uninstalls the app.
Does it have to be the SD Card since that's not always generally available on all phones?
External storage is "generally available" on all Android devices that have the Android Market. It might not be available in specific circumstances (e.g., it is mounted on a host PC, external storage was removable and was actually removed).
If you want to file not to be deleted on uninstall is not possible as you might know that uninstall will delete all the data.
While if you want to save the file on update of code you can use that same method as you are using by creating file on getFileDir(); just you have to check out each time before creating file that if file already exists or not.
If file exists there is no need to create again and if it is not there then create it.
I am assuming that you have done all stuff of file creating properly. Just add below code before creating it.
if(f.exists()) //Where f is your file
{
//Don't create the file, it already exists
}
else
{
//Create the file, since it didn't exist
}

Categories

Resources