Android create Folder and detect it - android

I want to create a folder to save a copy of an image from gallery
to use it from my Directory
I have tried a lot for how to save the image inside the folder and how to use it back.
The folder must be Private Folder so that only my app can access it.

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. For example:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available are: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.
it's take from:
How to create private folder in sdcard

You might want to take a look at the getDir() method that does exactly what you wish to accomplish..

Related

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)

Need to save image into the gallery folder of Phone Memory(Not SD Card)

I need to create a folder in the default gallery folder of the phone menory(Not SD Card).After that I need to save the Image into a folder(creating it in gallery folder) using the camera.
The problem is that I can find the default galley folder in the SDcard and I can create the folder and save images into it.
But with the phone memory I can't see the default gallery folder .Is there any way to see the gallery folder in the phone memory.And also how to create folder and save images into it.
Try to create folder in internal memory like this
File mydir = context.getDir("mydir", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, "myfile");
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
And than while saving file on folder,give path to this directory and Check O/P
this line add your image in gallery:
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
Note that your must also add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to your manifext.xml
You can create Folder and saved images into Internal Memory(Phone Memory). But,any one can't see those folder as well as image too. That is the purpose of internal memory in android
To create and write a private file to the internal storage:
1. Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.
2. Write to the file with write().
3. Close the stream with close().
For example:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
Refer the link...............
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

in which path should i need to put the text file to write in android

I have an android app and i need to write some string values in text file. i have coding to write in a text file. but i don't know where should i create my text file.Please refer my code below,
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput(“public.dat”, Context.MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(“TEXT”);
osw.close();
fOut.close();
}catch(Exception e){
e.printStackTrace(System.err);
}
please inform me in which path should i create the text file.
If you want to store data locally to your app, on the device's internal file system (not on external SD card), you should always consider SharedPreferences, which are, by default, private files in the private storage space of your app, with convenient access.
SharedPreferences are perfect for key/value pairs, but not very suitable for certain data formats. So if you have different needs, the Context class has a broad variety of methods on offer. Please note that both Activity and Application objects are Contexts so you'll always be able to access these. Please refer to the documentation:
getDir()
getFilesDir()
openFileOutput()
There are also methods to get the path where external SD cards are mounted on the particular device, for temporary files, etc.
The best way is to create the file in the directory of your application in sdcard.
fOut = openFileOutput(Environment.getExternalStorageDirectory().getAbsolutePath()+“/yourAppFolder/public.dat”, Context.MODE_PRIVATE);
I think it is not a best optimal solution but it is worth full if it is working..

write private file to internal storage

I trying to write some files on the internal storage.
I saw theFileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); on Data Storage and I understood that the file will be private to my application.
but the problem is it can only open a file without a path, so first I opened a new directory file with file.mkdir(), but now, how do I write the file as private ?
I saw theFileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); on Data Storage and I understood that the file will be private to my application.
Correct. They will be private by default.
but the problem is it can only open a file without a path, so first I opened a new directory file with file.mkdir(), but now, how do I write the file as private ?
Your files that you create in subdirectories off of in getFilesDir() are private by default -- you do not have to do anything special to make them be private.
If you call Context.openFileOutput() the file will always be written into your application's data directory (i.e. "/data/data/appname/filename"). You cannot use sub folders here!
The documentation for this function says
nameThe name of the file to open; can not contain path separators.

Sharing Temporary Files Between Apps with No SD Card

If I create a temp file in my application's cache directory I can't, for instance, email it as an attachment. This is because the other app doesn't have permission to read the file.
In API 9 and up, I can set the file to be world readable with setReadable(true, false). unfortunately, I am targetting API level 7+. I don't want to rely on the presence of an SD card, so I can't use the location returned by getExternalStorageDir() to store it in case no card is present.
How can I generate and share a temp file in a way that will work, even with no SD card present?
final String FILENAME = "file.tmp";
final String someData = "File data";
FileOutputStream fos = openFileOutput(FILENAME,
Context.MODE_WORLD_WRITEABLE |
Context.MODE_WORLD_READABLE);
fos.write(someData.getBytes());
fos.close();
http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String,%20int)

Categories

Resources