Can I create password protected folder in Android? - android

I would like to create a folder which is password protected. I am able to create a folder in the following way, but how can I create a password protected folder?
File nf = new File("/sdcard/NewFolder");
nf.mkdirs();

in android there is not possible but we can hide using "." at prefix
otherwise Store important folder or file in internal memory.
you can see you package directory following way
String dir = getPackageManager().getPackageInfo("com.example.kinkey", 0).applicationInfo.dataDir;
Note: it is visible on windows pc

You can't create such a folder on sdcard. Everything that's saved onto sdcard can be accessed by other applications. If you want to create a folder which is not accessible from outside of your application use Context.getDir() method with mode set to MODE_PRIVATE:
Context ctx = getContext();
File folder = ctx.getDir("NewFolder", Context.MODE_PRIVATE);

Related

Moving a file from a PC to an Android-readable location

I am creating a simple Android app. To run properly, it needs the contents of a file (example.xml) that is currently on my PC.
On creation, the app would look for the file, and then do some stuff with it.
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
File file = new File("I wish I knew what to put here");
doStuffWithFile(file);
}
Unfortunately, I don't know where to put the file.
I need a location in internal storage to which both my PC and my Android app have access.
Is there such a location? If so, what file path String would I use to access it?
You can get the Download folder by using Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
Then you can add a filename and use this e.g. with DownloadManager
You could include the file in the assets folder of your project and then retrieve it with this.getContext().getAssets().open("example.xml")

Where should I choose to save text file in android?

I hope to export my data as a text file and save it to disk in Android, so I need to choose which folder I will save the file to.
I hope that a normal user can find the folder easily and the app does not need special permission to create the folder.
I have read some document, it seems that there are 3 ways: Context.getFilesDir().getAbsolutePath(), Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null).
You know some android users don't install SD card, so it seems that Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) are be excluded.
Am I only to choose Context.getFilesDir().getAbsolutePath()? or is there a better way? Thanks!
BTW, From the document Android - Where to save text files to?
Save it in internal phone storage, here no users and applications can access these files(unless if phone is rooted). But these files will be deleted one's the user selectes clear data from Settings -> Apps -> .
It seems that normal users can't access the saved text files if I use Context.getFilesDir().getAbsolutePath(), is it right?
Use this if you want a path that the user can modify and can have access
getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
More documentation here.
EDIT:
This is how use in case error in some devices:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
String fname = "TEXT.txt";
File file = new File(path, fname);
if (!path.exists()) {
//noinspection ResultOfMethodCallIgnored
path.mkdir();
}
// YOUR CODE FOR COPY OR CREATE THE FILE TXT in PATH WITH THE VARIABLE file ABOVE

creating folder in android emulator

I am developing an android application. I need to create a folder in the internal memory, but when I try to create the folder I get the error below. I am running in an emulator.
mkdir failed for /mnt/New Folder , read only file system
I have tried many paths, but still the error persists. The only folder that I am able to create is called "cache", but I cannot browse it by my file chooser activity.
Any idea where is the suitable place to create folders without any permissions?
You can achieve it by this from a Context object (like Activity).
File files_folder = getFilesDir();
File files_child = new File(files_folder, "files_child");
files_child.mkdirs();
File created_folder = getDir("custom", MODE_PRIVATE);
File f1_child = new File(created_folder, "custom_child");
f1_child.mkdirs();
The function
getFilesDir()
will get the folder data/data/yourpackagename/files in internal memory. And the function
getDir("custom", MODE_PRIVATE)
will create a folder name app_custom in your app internal folder.
Answered by Minhtdh
I guess what you call internal memory is atualy the external memory (which can be open by
file chooser activity, the real internal memory only can be open if you have rooted)
If that true, you should chek those belows:
- first, you will need the write storeage permission in Manisfest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- then you should use `
String path =
Environment.getExternalStorageDirectory().getAbsolutePath() +
"/yourfoldername"
`
than
mnt/yourfoldername
at last you should use mkdirs to create folder than mkdir

I get file not found exception trying to open a linked file in my assets folder (android)

I have a file in my assests folder, that I imported using the 'link' option (as opposed to copy). I use the following code to open it, but I get a file not found exception.
super.onCreate(savedInstanceState);
setContentView(R.layout.bar_page_lo);
appContext = getApplicationContext();
InputStream ins;
ins = appContext.getResources().getAssets().open("bar-data.json");
I need it to be linked because the file is in a dropbox folder so that my colleague can edit it. is there something special I need to do to use a linked file?
try:
appContext.getResources().openRawResource(R.raw.bar-data);
File must be placed in raw folder.

Get preloaded file in android

I'm new to android development and I am working on a little project. What I am having some issue with is getting access to preloaded files.
In my app, I have an XML file that I preloaded (I just simply put it in my src folder in a package). How do I access them in my classes? I need to get a File object pointing to this file so that I can use it as I would I/O files. It seems like this should be trivial, but alas I am stuck.
Lets say the file is located under: com.app.preloadedFiles/file1.XML
I've tried something along the lines of this, but have had no success:
URL dir_url = ClassLoader.getSystemResource("preloadedFiles/file1.XML");
FIle file = new File(dir_url.toURI());
I solved this in my app by getting an InputStream to the file -- something like:
myContext.getAssets().open(fileName);
//read the data and store it in a variable
Then, if you truly need to do File related opterations with it, you can write it to a private (or public) directory and do your operations from you newly written file. Something like:
File storageDir = myContext.getDir(directoryName, Context.MODE_PRIVATE);
File myFile = new File(storageDir + File.separator + fileName);
//then, write the data to the file and manipulate it -- store the name for access via File later

Categories

Resources