Creating file directory to external storage - android

Good Morning,
I am trying to create file directory to external storage by using below code:
File noteDirectory = new File(Environment.getExternalStorageDirectory()+"/Notes");
noteDirectory.mkdirs()
My question is what happens if noteDirectory already exists on user's device?

Nothing happens, other than mkdirs() will return false. It will not crash with an error or anything like that.
BTW, please replace:
File noteDirectory = new File(Environment.getExternalStorageDirectory()+"/Notes");
with:
File noteDirectory = new File(Environment.getExternalStorageDirectory(), "Notes");

Related

Make a new folder on internal storage android

So I want to make a folder right in the main part of my internal storage. So basically the same area that you see when you plug your phone in your computer and you open up internal storage and see all those folders and files.
I've tried the following codes:
String path = Environment.getDataDirectory().getAbsolutePath().toString()+ "/storage/emulated/0/appFolder";
File mFolder = new File(path);
if (!mFolder.exists()) {
mFolder.mkdir();
}
Along with
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File myDir = contextWrapper.getFilesDir();
// Documents Path
String documents = "appFolder";
File documentsFolder = new File(myDir, documents);
documentsFolder.mkdirs(); // this line creates data folder at documents directory
Along with a few others I've found online, but none of these seem to make a folder or at least on the internal storage that I want.
I'm not sure if I'm missing something? But any help would be great, I've tried essentially copying a lot of posts I've seen online but none has even gotten me a folder? I don't get any errors either?
I have:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.name.app">
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
in my manifest, although I think I'm using gradle if that helps
-------------edit
Unfortunately I tried this from the url aswell, but still no folder.
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File mydir = contextWrapper.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
Okay somehow fixed the problem
Turns out if you do
File Directory = new File("/sdcard/folderName/");
// have the object build the directory structure, if needed.
Directory.mkdirs();
and use
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
it now works.
I'm not too sure if changing the permission from INTERNAL to EXTERNAL did it, or if it was the directory which is now just /sdcard/folder/or probably both, but either way it worked so I'm happy. Thanks all!!

Rename a file in the internal storage

What's the best/easiest way to rename a file in the application's internal storage? I find it a bit strange that there is a Context.deleteFile() method, but no "move" or "rename" function. Do I have to go all the way through saving the file's contents, deleting it, creating a new one and then copying the contents into that one? Or is there a way to copy a file over an existing file?
Update (Aug. 30, 2012):
As per the suggested solution below, which I cannot get to work:
I have a file called shoppinglists.csv
Then I create a new file called shoppinglists.tmp, and copy the contents from shoppinglists.csv AND some new entries into that. The shoppinglist.tmp file is then a new version of the shoppinglists.csv file
Then I delete the old shoppinglists.csv file
Then I need to rename the shoppinglists.tmp file to shoppinglists.csv
I tried this:
ctx.deleteFile("shoppinglists.csv"); <--- delete the old file
File oldfile = new File("shoppinglists.tmp");
File newfile = new File("shoppinglists.csv");
oldfile.renameTo(newfile);
However, this doesn't work. After deleteFile(), nothing more happens, and I'm left with the new shoppinglists.tmp file.
What am I missing?
NB: There are no errors or anything in LogCat.
Instead of using a raw File constructor, use the method getFileStreamPath provided by the Context. That is to say, do:
File oldfile = ctx.getFileStreamPath("shoppinglists.tmp");
File newfile = ctx.getFileStreamPath("shoppinglists.csv");
oldfile.renameTo(newfile);
renameTO() doesn't work in my environment (Eclipse Indigo, AVD with android version 2.3). The solution is to skip the temporary file method alltogeher, since it doesn't seem to be possible to solve in any reasonable time frame.
I think we cannot use File.renameTo() method in Internal Storage environment.
Renaming a file in this environment, can do:
- Copy content of the old file to new file.
- Delete old file.
File file = new File("your old file/folder name");
File file2 = new File("your new file/folder name");
boolean success = file.renameTo(file2);

Find path of storage in Android

The application that am working has to access the files stored
on a tablet in the internal storage folder. Can any one tell me
how to get access to it?
What about this?
return Environment.getExternalStorageDirectory().toString() + "/Music";
See internal storage section of android data storage document..
EDIT
I think you should use ContextWrapper..Personally I never tried this..
ContextWrapper cw = new ContextWrapper(context);
File directory = cw.getDir("your music folder name here", Context.MODE_PRIVATE);
please check directory is null or not before you use it..
EDIT
See this thread..this might help you
You can use :
File f = new File(Environment.getExternalStorageState());

Writing hidden file .nomedia fails on internal storage

I want to create .nomedia file in the internal cache directory where I will store images and tried the following..
File dir = getCacheDir();
File output = new File(dir, ".nomedia");
boolean fileCreated = output.createNewFile();
When I try this fileCreated is false and the file doesn't get created.
If I used nomedia without a dot the file gets created but this is no use as the MediaScanner is looking if the .nomedia file exists.
I also tried this with a FileOutputStream and to write data in the file in case it was because I was only created an empty file but this doesn't work either.
Anyone have an idea why this could be failing?
Hmm, are you really sure that the file not already exists? Please note that you need to do an ls -a to see a file starting with a dot.
The documentation of File.createNewFile() says:
Returns true if the file has been
created, false if it already exists.
If the file is not created for other reasons (i.e. security), it should throw an exception.

How to create a file on Android Internal Storage?

I want to save a file on internal storage into a specific folder. My code is:
File mediaDir = new File("media");
if (!mediaDir.exists()){
mediaDir.createNewFile();
mediaDir.mkdir();
}
File f = new File(getLocalPath());
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(data);
fos.close();
getLocalPath returns /data/data/myPackage/files/media/qmhUZU.jpg but when I want to create the media folder I'm getting the exception "java.io.IOException: Read-only file system". Any ideas how to write my files on internal phone storage in in folder media? Thanks.
You should use ContextWrapper like this:
ContextWrapper cw = new ContextWrapper(context);
File directory = cw.getDir("media", Context.MODE_PRIVATE);
As always, refer to documentation, ContextWrapper has a lot to offer.
You should create the media dir appended to what getLocalPath() returns.
I was getting the same exact error as well. Here is the fix. When you are specifying where to write to, Android will automatically resolve your path into either /data/ or /mnt/sdcard/. Let me explain.
If you execute the following statement:
File resolveMe = new File("/data/myPackage/files/media/qmhUZU.jpg");
resolveMe.createNewFile();
It will resolve the path to the root /data/ somewhere higher up in Android.
I figured this out, because after I executed the following code, it was placed automatically in the root /mnt/ without me translating anything on my own.
File resolveMeSDCard = new File("/sdcard/myPackage/files/media/qmhUZU.jpg");
resolveMeSDCard.createNewFile();
A quick fix would be to change your following code:
File f = new File(getLocalPath().replace("/data/data/", "/"));
Hope this helps
Write a file
When saving a file to internal storage, you can acquire the appropriate directory as a File by calling one of two methods:
getFilesDir()
Returns a File representing an internal directory for your app.
getCacheDir()
Returns a File representing an internal directory for your
app's temporary cache files.
Be sure to delete each file once it is no longer needed and implement a reasonable
size limit for the amount of memory you use at any given time, such as 1MB.
Caution: If the system runs low on storage, it may delete your cache files without warning.
Hi try this it will create directory + file inside it
File mediaDir = new File("/sdcard/download/media");
if (!mediaDir.exists()){
mediaDir.mkdir();
}
File resolveMeSDCard = new File("/sdcard/download/media/hello_file.txt");
resolveMeSDCard.createNewFile();
FileOutputStream fos = new FileOutputStream(resolveMeSDCard);
fos.write(string.getBytes());
fos.close();
System.out.println("Your file has been written");

Categories

Resources