Problem to create directory in Android - android

I'm developing a simple application for Android devices.
I need to create a directory, but I can't do it.
File folder = new File(Environment.getExternalStorageDirectory ()+"/dir");
if(folder.mkdirs()){
CrearToast("Directorio creado"); //successfully created
}else{
CrearToast("fallo"); // error creating directory
}
*CrearToast creates a toast with the text in brackets.
I have set
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

are you trying to write on SD card or phone memory??
Have you looked at this??
http://developer.android.com/guide/topics/data/data-storage.html
EDIT:
This is how i create my folders
File CheckDirectory;
CheckDirectory = new File(FolderPath);
if (!CheckDirectory.exists()){
CheckDirectory.mkdir();
}
SD card directory:
Environment.getExternalStorageDirectory().getAbsolutePath() + "/";

Related

Create a file folder for app

The below code doesn't create a folder in my device.
String intStorageDirectory = context.getFilesDir().toString();
File folder = new File(intStorageDirectory, "test");
folder.createNewFile();;
I need a folder created for my app to store media, when user installs it. That folder should be visible on file explorer. How can i do it?
With the current snippet you created a file, you can also create folder by creating file but your current directory is the base folder, getFilesDir() points internal storage for your app which not visible nor accessible unless explicitly declared. You can create a folder and file by creating with new File().createNewFile() or create only folder using mkdirs() but you won't be able to display it using a file explorer app and that folder and files inside it will be deleted when/if user uninstalls your app.
To save files externally(This doesn't mean saving to SD Card) you can create directory and file with
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), folderName);
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs()
}
File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + fileName);
And you need some kind of OutputStream to write data to that file.
Make sure that you ask <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> inside your AndroidManifest.xml file and ask write permission on runtime if your android:targetSdkVersion="23" or above
new File(context.getFielsDir(), "test").mkdirs();
createNewFile creates a file, not a folder. Using mkdirs instead of mkdir ensures that all parents exist. There's also no reason to go through a string when you already have a File.
Adding folder.mkdirs(); should work in place of folder.createNewFile(); And don't forget to add the permissions.
This will create a folder in you data directory.
And just a suggestion , if you want to store media in a SD card folder maybe Environment.getExternalStorageDirectory() is good.

I can't write to the external SD card(extSdCard) in Android Studio

I have created an android app that needs to create a folder and write text files on my external SD card(extSdCard). I am using the Galaxy S4 device and have written the following codes for that. I already know the path of /mnt/.. file and have created a string for it. The android manifest.xml file uses the permission.i have checked the codes in "adb logcat" in Cmd prompt and it does not give any error but doesn't create any folder. The device has also been checked unconnected with the PC. Would appreciate if you help me. Here is the code.
String externalFilePath="/mnt/extSdCard/tmp";
Log.d(TAG, "externalFilePath is: "+externalFilePath);
File myfile = new File(externalFilePath, "Hello");
First of all make sure that you have this line inside your manifest,xml, somewhere outside application tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then you can write a File doing this :
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + "/tmp/");
// creates if doesn't exists
dir.mkdir();
// create a File
File file = new File(dir, "Example.txt");
FileOutputStream os = outStream = new FileOutputStream(file);
//this is the text that will be inside of the Example.txt
String data = "Hello world";
os.write(data.getBytes());
os.close();
Hope it helps :)
try this code to generate files under your application package
File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
// Replace DIRECTORY_PICTURES with your needs
File file = new File(path, "Hello");
Also make sure you have added the permissions

File creation in SD card not working

I am trying to create a file and store it in SD Card to be used as an input for some processing for an apps.
After searching for a while, I got this code which can create a file in SD card.But after running this,I couldn't see any file created in my SD card. Can anyone please help me what I am missing here.
BufferedWriter out = new BufferedWriter(new FileWriter(FileDescriptor.err));
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()) {
File perffile = new File(root, "samplefile.txt");
FileWriter perfwriter = new FileWriter(perffile, true);
out = new BufferedWriter(perfwriter);
}
} catch (IOException e) {
Log.e(TAG, "-Could not write file " + e.getMessage());
return;
}
If you want to add a file or folder or move application into your SD Card just do the following:
steps:
1) Open your Android application's source code file with a text or programming editor.
2) Browse to the location in the source code where you wish to call the function that writes a file to the device's external storage.
3) Insert this single line of code to check for the SD card:
File sdCard = Environment.getExternalStorageDirectory();
4) Insert these lines of code to set the directory and file name:
File dir = new File (sdcard.getAbsolutePath() + "/folder1/folder2");
dir.mkdirs();
File file = new File(dir, "example_file");
// The mkdirs funtion will create the directory folder for you, use it only you want to create a new one.
5) Replace "/folder1/folder2" in the above code with the actual path where you intend to save the file. This should be a location in which you normally save your application files. Also, change the "example_file" value to the actual file name you wish to use.
6) Insert the following line of code to output the file to the SD card:
FileOutputStream f = new FileOutputStream(file);
Finally step 7:
Save the file, then compile it and test the application using the Android emulator software or the device.
This will works!!! ;-)

How to check if newly created folder is present into SD Card in Android

From my application, I want to store some images into my SD card. For that I need to create a one folder.
At the first time folder will create but after it checks whether that folder is present or not. How can I do it?
below code will create a directory if it does not exist
File direct = new File(Environment.getExternalStorageDirectory() + "/New Folder");
if(!direct.exists())
{
if(direct.mkdir())
{
//directory is created;
}
}
You should request the following permission first in your Android manifest :
android.permission.WRITE_EXTERNAL_STORAGE
and execute above code by Rasel for it to work.

save image to sdcard android Directory problem

Im trying to save data to sdCard first i tried to saave it privately within app directory on externalStorage using getExternalFilesDir but gives me nullPointerException so i tried the other way given below it worked but when i want to store files into a custom directory that i want to named myself it give me error:
FileOutputStream os;
dirName = "/mydirectory/";
try {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)){
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + dirName);
dir.mkdirs();
//File file = new File(this.getExternalFilesDir(null), this.dirName+fileName); //this function give null pointer exception so im using other one
File file = new File(dir, dirName+fileName);
os = new FileOutputStream(file);
}else{
os = context.openFileOutput(fileName, MODE_PRIVATE);
}
resizedBitmap.compress(CompressFormat.PNG, 100, os);
os.flush();
os.close();
}catch(Exception e){
}
ErrorLog:
java.io.FileNotFoundException: /mnt/sdcard/mvc/mvc/myfile2.png (No such file or directory)
Your directory "/mnt/sdcard/mvc/mvc" may not exist. What about changing your path to store the image in the Environment.getExternalStorageDirectory() path and then working from there?
Also, as Robert pointed out, make sure you have write permission to external storage in your manifest.
Edit - to create directories:
String root = Environment.getExternalStorageDirectory().toString();
new File(root + "/mvc/mvc").mkdirs();
Then you can save a file to root + "/mvc/mvc/foo.png".
Have you requested permission to write onto SD card? Add the following string to you app manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You should check if you have added the required permission android.permission-group.STORAGE to your app. Without that permission you won't be able to access anything on the SD-Card.
BTW: On the Android system I know the SD-card is mounted on /sdcard not /mnt/sdcard
I found this book to be very helpful: "Pro Android Media: Developing Graphics, Music, Video, and Rich Media Apps for Smartphones and Tablets". I noticed a part that allows saving images and stuff to the SD card.

Categories

Resources