I want a develop simple app in which i have to check at the time of my app installation that directory that i create in my app is already exist before the app is installed, if yes then i have to remove that directory and create new directory in the sdcard.
I am able to do it at installation time but the actual problem is that i put that code in my main activity so when i reopen my app at that time directory is deleted also , so that i don't wont to do because i have put data on that after first installation .
If anyone know the way then please tell me ..
Kind regards,
Jalp.
File dir = new File("/sdcard/dirname/");
if(!(dir.isDirectory() || dir.exists())) dir.mkdirs();
File wallpaperDirectory = new File("/sdcard/dirname/");
// have the object build the directory structure, if
// needed.
if(!wallpaperDirectory.exists())
{ wallpaperDirectory.mkdirs();}
please try this
Related
This is my question.
First I have create my personal folder on create
File parentFolder = new File(MainApplication.getInstance().getExternalCacheDir().getAbsolutePath()
+ File.separator+"myfolder");
if (!parentFolder.exists()) {
parentFolder.mkdirs();
}
Second in my application,i can receive file such as png from another application,and the file that i received has been saved in /Android/data/packageName/cache/myfolder/hashcode/example.png. And i choose the gallery to open it from intent chooser.
When gallery is open and I can see the png file.I kill my application's process and uninstall it.
Finally I install my application again.The path /Android/data/packageName doesn't been created! And create function show that
MainApplication.getInstance().getExternalCacheDir();
returns null!
Give me some advise! Thank you.
Try using
new Context().getApplicationContext().getExternalCacheDir().getAbsolutePath()
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
could you help me about this?.When the users opens the app or install the app the process is to automatically move files in a certain folder. Guys, any suggestion, codes or links are helpful and appreciated ... :D
Thanks in advance.
For move file you can use below code:
File from = new File(Environment.getExternalStorage().getAbsolutePath()+"/folder1/file.ext");
File to = new File(Environment.getExternalStorage().getAbsolutePath()+"/folder2/file.ext");
from.renameTo(to);
just use this method for folders that be on same mount point.
I've used application folder
data/data/com.xxx.xxx/databases/Customer.db
to store a database,it works fine and i could open and used it,but i wanted to add multiple folders to this path like
/data/data/com.xxx.xxx/databases/b36f6e58-0971-4f79-aca0-dada4201d886/Customer.db
but when i download the database and put it on the path and when i want to open it, it throws Exception that couldn't open the database.i have also try downloading the db ,check and makeDir the path then move db to the path but it doesn't solve the issue.
is there anything wrong with adding another folder in application folder or am i missing something? Any help would be appreciated.
Try this way:
File newDir = new File(getFilesDir(), "newDir");
if (!newDir.exists()) {
newDir.mkdirs();
}
File inside newDir can be accessed with openFileInput/openFileOutput. For both you need a context
I have a question about Android programming. Basically, I am unsure of where to check where my file is, and if I wrote to it correctly. I want to locate where the file is, and I also want to know whether or not I wrote to it correctly. Below is the code I have come up with:
String lsNow = "testing";
try {
fos = openFileOutput("output.txt", Context.MODE_APPEND);
fos.write(lsNow.getBytes());
fos.close();
}
catch{
...
}
Where can I find output.txt? Might anyone know how to check this all out? if so, that would be great! I am using an emulator by the way. If I were to do this on a real Android, how would one approach this also? (Just for future reference)
You Test it in Two ways
Using File Explorer
Go to DDMS perspective--> Open File Explorer-->location of the file
Pragrammatically by using exits() method
File file = new File(context.getFilesDir(), filename);
if(file.exists())
Using openFileOutput(...) means the file will be written to internal storage on the Android device in an area which is secure from access by other apps.
If you want to make sure the file is written correctly then make sure your catch block handles any failures (if it is called then the file writing has failed).
To access the file once it has been written use openFileInput(...).