Android Create Directory to device - android

I use the following to check if a directory exists and if it doesn't it then creates it
final String appPath = String.format("%s/DataFiles", Environment.getExternalStorageDirectory());
File f = new File(appPath);
if(f.exists() && f.isDirectory()){
Toast.makeText(getBaseContext(), "Exists", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getBaseContext(), "DOES NOT Exist", Toast.LENGTH_LONG).show();
f.mkdir();
This works fine on my S3 that has a sd card inserted
When I try it on anither phone that doesnt have an sd card in it recognises that the directory doesnt exists and then doesnt create the directory
Any ideas whats wrong
Mark
BTW I have tried inserting an sd card but doesn't make a difference it still doesn't create directory.

Before any operation you need to check if external storage is available.
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
if sd-card is not available or is not available for writing you need to use device storage:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Related

Android doesn't create folder on my Nexus

I have a Nexus 9 device. I would create a personal folder in the /sdcard/ path, something like this:
/sdcard/MyFolder/
so i coded this:
File directory = new File("/sdcard/MyFolder/");
if(directory.mkdirs()){
Toast.makeText(context, "Folder created", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Folder not created", Toast.LENGTH_SHORT).show();
}
When I launch the app, it shows me "Folder not created" and so it doesn't create folder called MyFolder into /sdcard/ path. In Nexus 9 device the /storage/emulated/0path doesn't exist, so I have to use /sdcard/ path to accessing my storage.
I also used permission in my AndroidManifest.xml file, in this way:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Can you tell me please what's wrong?
Thanks
Since lots of devices have different file structures it is safer to use Environment.getExternalStorageDirectory() for determining the SD card path.
Can you try this code
File folder = new File(Environment.getExternalStorageDirectory() + "/MyFolder");
if (!folder.exists()) {
folder.mkdir();
}
try to specify external storage as listed below:
File directory = new File(Environment.getExternalStorageDirectory().toString()+"/MyFolder");

Nothing Happening when trying to write to file on SD card android

I am using the following code to write to an SD card:
File dir =new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");
if(!dir.exists())
{
dir.mkdirs();
}
String filename= "MyDoople.txt";
try
{
File f = new File(dir+File.separator+filename);
FileOutputStream fOut = new FileOutputStream(f);
OutputStreamWriter myOutWriter = new OutputStreamWriter(
fOut);
myOutWriter.append("Mytest");
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Text Updated",
Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
However when I run my app, and then go check in the SD card, there is nothing there. Why am I not seeing the file that I created? I am using android jellybean 4.1 and have added the write permissions in the manifest file.
From your code, you're writing to the folder "MyFolder" under primary external storage.
What is the device you are using? Does it have interal storage in additional to sd card? If yes, then your file is written to the internal storage, but not the sd card.
Edit:
To access SD Card, you simply replace android.os.Environment.getExternalStorageDirectory() with the sd card path.
It is not an easy task to find the path of SD card.
One method is to use ContextCompat.getExternalFilesDirs(context, null), the first element of the returned value would be the same android.os.Environment.getExternalStorageDirectory(), the second element would be somewhere of the sdcard.
However, could be depending on your android version, the directory returned could be a sub-directory on the sd card, i.e. your application specific directory instead of the root of SD card. You have to check and manually change it if you want to find the root directory.
http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getExternalFilesDirs%28android.content.Context,%20java.lang.String%29

Unable to read directories or files from a sd card

I am new to android development and I have tried to read a directory from my sd card but didn't got succeeded. Below is the code which I wrote to achieve it
File sdcard = Environment.getExternalStorageDirectory();
if(sdcard.exists()) {
Log.d("Sd card", "Sd card exist");
File[] file_names = sdcard.listFiles();
for(File x : file_names) {
Log.d("File Name",x.getName());
}
}
Control passed the if condition and then I got an NullPointerException at "for each" loop line. Probably function sdcard.listFiles() is returning null. I have an Sd card with many folders and files. Actually I have to create a directory file object for directory "Attachments" which is available on my sd card. I also tried the following code to achieve the same.
File file = new File(android.os.Environment.getExternalStorageDirectory() + File.separator + "Attachments");
and
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Attachments");
on both code when I called the function file.exists(), it returned false.
I have also checked whether sd card is mounted or not by the following code.
String sdCardAvail = Environment.getExternalStorageState();
if(sdCardAvail.equals(Environment.MEDIA_MOUNTED))
Log.d("Card status", "Sd card Available");
The above code has printed "Sd card Available" at log cat.
So please help me out to know whether I am doing it correct or missing any thing.
Thanks....
In order to read or write files on the external storage, your app must acquire the READ_EXTERNAL_STORAGEorWRITE_EXTERNAL_STORAGE system permissions. For example:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
If you need to both read and write files, then you need to request only the WRITE_EXTERNAL_STORAGE permission, because it implicitly requires read access as well.
Also See This Documentation. It Will solve all your future regarding Storage problems.

Create a folder and write into it in the root of android

I'm not trying to write into the external sd at /mnt/sdcard. I'm trying to create a folder for may app files and have them accesible by others.
I have an app called Libra that generates .csv files when exporting data and all af it goes to /Libra/ folder. I want my app to do the same.
As far as I've seen there's external storage which is the sd and internal which is a non public place for the app.
How can I make a dir at the root of the android file system as Libra does ?
If I ask for the external storage I get the following non desired locations :
Environment.getExternalStorageDirectory()
/storage/emulated/0/ (in my Nexus)
/mnt/sdcard (in the emulator)
If I try to make the folder in an absolute path /MyDiary It returns Error creating folder
File folder = new File("/MyDiary");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
Toast.makeText(getBaseContext(), "Already exists", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Error creating directory", Toast.LENGTH_LONG).show();
return;
}
If I try to check if the Libra folder does exists, it says it doesn't exist:
File folder = new File("/Libra");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
Toast.makeText(getBaseContext(), "Libra exists", Toast.LENGTH_LONG).show();
}
Note: I've got the manifest permissions to write in external storage.
The root is a ramdisk. And the folder in it some are created by init.rc. Only init.rc have permission to create the folder in the ramdisk.
If you create the folder by init.rc, you still need init.rc help to mount a true stroage device on the new folder.
Repack like The init.rc file regenerated on restart
First of all you need root access Permissions to write.
You can see whether it has created folder there or not using rootexplorer app.
Try this after getting root access.
I don't think that's the right way to create a File in the Android OS.
From http://developer.android.com/training/basics/data-storage/files.html:
If you want to save public files on the external storage [I've read your advice, but I think this way you can access files from multiple apps, if that's what you want], use the getExternalStoragePublicDirectory() method to get a File representing the appropriate directory on the external storage. The method takes an argument specifying the type of file you want to save so that they can be logically organized with other public files, such as DIRECTORY_MUSIC or DIRECTORY_PICTURES. For example:
public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
try first getting superuser permission to your app.Run linux commands for making folder and files into which you want to write data(You can do this with only linux commands)just at 1st run of your app.Then you can write data to files by getting FileOutputStream("filename") object.Leave ur remarks if it worked or not.
partial code for above procedures is
Process p=Runtime.getRuntime().exec("su");
OutputStream o=p.getOutputStream();
o.write("mkdir foldername\n".getBytes());
o.write("cd foldername\n".getBytes());
o.write("cat > filename\n".getBytes());
o.write("exit\n".getBytes());
o.flush();
p.waitFor();

Tracking a path of sd card file programatically

I have put html file in sd card on android device. and i am running that through webview. But the path that i am giving in emulator is working fine. but not on device. Its giving an error ** Web Page on Found** while running.
I have this code to find the sd card availability and sd card root directory path and it is working fine. and output is coming properly.
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
Toast.makeText(this, "yes SD-card is present", Toast.LENGTH_SHORT).show(); }
else
{
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
}
File externalStorage = Environment.getExternalStorageDirectory();
externalStorage.getAbsolutePath();
Toast.makeText(this, externalStorage.getAbsolutePath(), Toast.LENGTH_SHORT).show();
but can i get the proper whole file path. Please suggest.
this code definitely solve your problem
String storage_path = Environment.getExternalStorageDirectory().toString()
+ File.separator
+ vfile;
also if you want to add some files in sd card then use this permission in your manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
may be this can be help you, try it.
String path = Environment.getExternalStorageDirectory().toString() + "/Filename_with_extension" ;

Categories

Resources