android : best way to open a file in temprory memory - android

I have a encrypted file, which on decryption is to opened in temporary memory, and when user closes the file they temporary file is deleted, What's the way to achieve this functionality?

You cannot use external storage for the file since it will be available to whole world(what if user ejects SD card before you delete it?)
You should use internal storage so that you can remove it once the user is done. Use MODE_WORLD_READABLE so that user/other apps can only read it. When user is done, you can delete it.
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes());
fos.close();
File file = new File(FILENAME);
file.delete();

Good practice
Use internal storage and check out this: getCacheDir()
Bad practice
create the file on your sdcard
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "filename.ext");
try {
outStream = new FileOutputStream(file);
//write data;
outStream.flush();
outStream.close();
}
delete the file
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "filename.ext");
boolean deleted = file.delete();

As LAS_VEGAS said using external storage is a terrible idea!!
See this link for cache getCacheDir()
http://developer.android.com/guide/topics/data/data-storage.html#InternalCache
or use the normal internal storage with MODE_PRIVATE
ie
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
then after wards
deleteFile(FILENAME);
EDIT: MODE_PRIVATE would make it not accessible by other applications so maybe not a good idea !:(
But also if it has to decrypt a file and save it unencrypted, if someone really wanted at that file they could get it I'm sure since you're having it world readable for a while

Related

How to create hidden directory in android?

I am working on an application where I have created some directory which I am accessing through my application I want to make that directory hidden for security purpose .Such that the user can access them only within the application does not access them outside the application as like through file manager.
Any help is appreciated.
Don't make it duplicate because I search out all the answer, but no one has worked for me.
Just appending a dot before the folder name will not protect it. It is only invisible to the user. It can still be accessed from apps, including file managers and therefore the user. It's just hidden by most file managers by default.
As you want to hide the files for security purposes, you should use Android's internal storage.
From the official Android developer guide:
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
Example:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
Android developer guide
You could also encrypt your files and store the encryption key in the android Keystore.
Here is a good answer regarding encryption of files in android.
Official guide regarding the Android Keystore.
Be clear "You want to create directory or folder which is not accessible for other application"(Which is your application folder) Or Create Folder any location but it is hide from your
For First Solution is -
public static File saveFileInAppDirectory(Context context,byte[] inpute, String directoryName,String fileName){
File mypath;
File directory = new File(context.getExternalFilesDir(
Environment.DIRECTORY_PICTURES), directoryName);
if (!directory.mkdirs()) {
directory.mkdir();
}
mypath = new File(directory, fileName);
try {
InputStream is = new ByteArrayInputStream(inpute);
FileOutputStream f = new FileOutputStream(mypath);
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.e("SAVE_IMAGE", e.getMessage(), e);
e.printStackTrace();
}
return mypath;
}
It will create Directory of your app folder Path - Android/Data/Data/Your.Package.Name/FolderName/FileName
For second Solution - just change file name
File mypath = new File(directory, "."+fileName);
If you want to achive both than just replace
new File(directory, fileName); with new File(directory, "."+fileName);
just write the directory name followed by a dot(.)
example:
.myDir or .myDir1
so these directories will not be visible through file manager. And while accessing these directories call them using dot(.) only
example:
"/path/to/folder/.myDir/"
same can be done for filename
For Hiding Folder in Android
Name of your folder is MyApplicationFolder then u need to add (.)Dot in front of the folder name like .MyApplicationFolder.
So When the Folder is created then the folder is hidden mode for images,video,etc inside but it will be visible in FileManager.

Create a file in Android with certain permission

I'm developing an Android application I have to implement a function that create a folder with different files.
I wish the files in the folders were hidden, and this point is not a problem, but I want assign certain permission to files, for example I need that files are not readable / writable by user but only from application.
Also I wish the files in the folder were deleted if the application was uninstalled.
This is the code that i use to create hidden folder:
File JSONStorage = new File(Environment.getExternalStorageDirectory(), ".BMA");
if (!JSONStorage.exists()) {
if (!JSONStorage.mkdirs()) {
Log.wtf("log: ", "Failed to create directory");
}
}
From official Android documentation:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
This will create file on device internal memory. This file can be read only by application. (Context.MODE_PRIVATE)

How to make folders of application in SD card are not accessible by file manager and other applications?.

I am creating two folders to store images of my application in SD card.but these folder are visible in file manger.i want to prevent access of my folder from outside.i am saving images in that two folders.i am saving like this please help me any one.
File sdcard = Environment.getExternalStorageDirectory();
File pictureDir = new File(sdcard, "Image_dir");
if (!pictureDir.exists()) {
pictureDir.mkdirs();
}
// saving the image file in the folder Image_dir
File f = null;
f = new File(pictureDir, file_name);
FileOutputStream fos = new FileOutputStream(f.getAbsolutePath());
Bitmap image;
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
normally adding a . in front of the name of the folder which will exclude it from media scanners. But with a file manager can still find it.
If you are working on Api Level-8 then I suggest to use getExternalFilesDir (String type)
instead of Environment.getExternalStorageDirectory() because there are two benefit using this
all images and data will be automatically deleted when your application uninstall by user
images do not visible in the media application (in Gallery).

How to Save Recorded Audio file in application's raw folder

I am creating an application in which I have to record the voice and save it and later play the same. I want to save the recorded audio in the raw folder of the application. How should I give the the directory path of raw folder to save my Audio file.
Sorry you can't.
Because of Android .apk file is read-only, you can't write any file in package hierarchy (like res, raw, asset).
Better to store it in application's package (internal storage) or External Storage.
Update:
This will create a file in application's internal storage.
//content is base64 string you can use directly use FileInputstream's byte
byte[] pdfAsBytes = Base64.decode(content, 0);
//filePath just filename of your audio file this will create a file in /data/data/<package_name>/files
FileOutputStream fos = openFileOutput(filePath, Context.Context.MODE_PRIVATE);
fos.write(pdfAsBytes);
fos.close();
You can't re-write resources after building the app. Best thing you can do with files placed in resources is read them into memory,modify them maybe, and save the results to sdcard or some other provided storage area. Files created by your application are also removed when your application gets un-installed, so need to worry.
To save to sdcard, try something like this:
You need the permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Then you can send in the data and the filename:
public void saveFileToCard(byte[] data,String filename) {
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// handle exception
} catch (IOException e) {
// handle exception
}
}
This, or a variation of it should help you i think.

save raw file into internal storage

I have a text file in raw folder and now i want to save it somewhere in the internal storage in android platform so that it is non-accessible.
Please if someone can help me out. Thanx in advance.
Something like this should work
InputStream is = new FileInputStream("path_to_your_file");
FileOutputStream fos = openFileOutput("your_file", Context.MODE_PRIVATE);
// read from InputStream and write ot OutputStream
More info about InternalStorage

Categories

Resources