Cannot create directory on sdcard :S - android

I have this code
File dir = new File(Environment.getExternalStorageDirectory() + "/" + "new_dir");
if (dir.mkdir()) {
txtView.setText(dir + " Directory created");
} else {
txtView.setText(dir + " Directory is not created");
}
and i also added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
but it always goes in the else and the directory is never made :S :S

Try This code
String newFolder = "/myFolder2";
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(extStorageDirectory + newFolder);
(!myNewFolder.mkdir())
{
Log.e(TAG, "Create dir in sdcard failed");
return;
}
User permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Related

Creating directories on certain Android versions

I tested this code with Genymotion Marshmallow and with Nougat on my HTC 10, and it worked on both.
Now I tried Android 7.0 on Genymotion and it didn't create the directories.
Any idea why?
File file = new File(Environment
.getExternalStorageDirectory() + File.separator +
"SchoolAssist" + File.separator + lesson_name);
boolean isDir = file.exists();
if (!isDir)
isDir = file.mkdirs();
if (isDir) {
Intent notes = new Intent(getActivity(), NotesManager.class);
notes.putExtra("dir", file.getAbsolutePath());
startActivity(notes);
}
else
Toast.makeText(getContext(), "Error creating directory", Toast.LENGTH_SHORT).show();
Edit: My manifest contains these lines:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In your code the Toast will be shown even when creating the directory worked. The Activity will only be started when the file already existed before calling your code.
Try this:
File file = new File(Environment
.getExternalStorageDirectory() + File.separator +
"SchoolAssist" + File.separator + lesson_name);
if(!file.exists()) {
if(file.mkdirs()) {
startNotesManager();
} else {
Toast.makeText(getContext(), "Error creating directory", Toast.LENGTH_SHORT).show();
}
} else {
startNotesManager();
}
And implement this helper method for starting the Activity:
private void startNotesManager() {
Intent notes = new Intent(getActivity(), NotesManager.class);
notes.putExtra("dir", file.getAbsolutePath());
startActivity(notes);
}

How to create folder in external storage?

This is what I tried.
private void createFolderInExternalStorage() {
String storagePath = System.getenv("SECONDARY_STORAGE");
Log.e("storagePath->",storagePath);
String path = "not available";
if (storagePath != null) {
Log.e("Path->", "" + storagePath);
File file = new File(storagePath.toString());
Log.e("readable->", "" + file.canRead());
Log.e("writable->", "" + file.canWrite());
Log.e("executable->", "" + file.canExecute());
dir = new File(storagePath + File.separator+etFolder.getText().toString());
if (!dir.exists()) {
dir.mkdirs();
Toast.makeText(this,"Folder "+etFolder.getText().toString()+" created",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"Folder "+etFolder.getText().toString()+" already exists",Toast.LENGTH_SHORT).show();
}
path = dir.getPath();
} else {
Toast.makeText(this,"External Storage not available",Toast.LENGTH_SHORT).show();
}
tv.setText("External SDCARD path->" + path);
}
if Secondary storage is present then System.getenv("SECONDARY_STORAGE") return /storage/sdcard1 in my case but getting following:
03-21 12:02:26.827 14155-14155/com.think.teststorage E/readable->: false
03-21 12:02:26.827 14155-14155/com.think.teststorage E/writable->: false
03-21 12:02:26.828 14155-14155/com.think.teststorage E/executable->: false
Even in some devices getting the above status as true but folder creation fails.
I have added the permission:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Suggestions are welcome.
You can use this code to create a folder.
File dir = new File("path/of/your/folder");
try{
if(dir.mkdir()) {
System.out.println("Folder created");
} else {
System.out.println("Folder is not created");
}
}catch(Exception e){
e.printStackTrace();
}
Add this permission also :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
For further reference : see this link
Let me know if this works for you! :)
Use the following lines:
//Define the path you want
String path = Environment.getExternalStoragePublicDirectory(Environment.YOUR_DIRECTORY) + File.separator + "YourFolderName";
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
YOUR_DIRECTORY is the directory where you want to create the folder, for example: DIRECTORY_DOCUMENTS, DIRECTORY_DOWNLOADS, DIRECTORY_PICTURES etc.
In your manifest should to add permission for write:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Hope it help!
It is very simple and straightforward in android
`
File mFolder = new File(Environment.getExternalStorageDirectory(), "Folder_Name");
if (!mFolder.exists()) {
mFolder.mkdirs();
mFolder.setExecutable(true);
mFolder.setReadable(true);
mFolder.setWritable(true);
}
`
Also include required permissions in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Just put these lines of code,
// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
and add require permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Does not work on KitKat, external, physical sdcard ??
then use use
Environment.getExternalStorageDirectory().getPath();

mkdir return false in android?

This is my code
String DATA_PATH="/mnt/sdcard/";
String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };
for (String path : paths) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.v("", "ERROR: Creation of directory " + path + " on sdcard failed");
} else {
Log.v("", "Created directory " + path + " on sdcard");
}
}
}
I've tried using Environment.getExternalDirectory() but it still return false. The most confusing thing is it always said "ERROR: Creation of directory mounted on sdcard failed" on the logcat. How can the path changed into mounted? Can someone please give me a solution?
if the mobile is connected to the system then we are not able to create folders so remove it and run the application
Why are you using mkdirs in the first place ? You should just do:
File file = new File(Environment.getExternalStorageDirectory(), "tessdata");
if (!file.exists()) file.mkdir();
Also, make sure you have the WRITE_EXTERNAL_STORAGE permission in the manifest.

how to read and write file on android 4.4?

I need to open gallery to show images in a specific directory,after a search I follow this
How to open gallery to show images in a specific directory
but the file.list returns me a null String[] on android 4.4
Than I write some test code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listDirectory("ExternalStorageRoot",
Environment.getExternalStorageDirectory());
listDirectory(
"DCIM",
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
writeFileTest();
}
private void writeFileTest() {
String tag = "WriteTest";
String path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath();
path = path + "/testFile.txt";
Log.d(tag, "path : " + path);
File file=new File(path);
FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write("THIS IS A TEST LINE".getBytes());
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void listDirectory(String tag, File f) {
Log.d(tag, "absolute path " + f.getAbsolutePath());
Log.d(tag, "is dir " + f.isDirectory());
Log.d(tag, "can read " + f.canRead());
Log.d(tag, "can write " + f.canWrite());
String[] fileNames = f.list();
Log.d(tag, fileNames == null ? "fileNames is null"
: "fileNames is not null");
if (fileNames != null) {
for (String string : fileNames) {
Log.d(tag, "file -- " + string);
}
}
}
I also add these lines in AndroidManifest.xml
<permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
TO:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
EDIT:
I mess up the permission.It's Now every thing works fine!
4.4 changed the file permissions for the sd card. You can't write outside of your personal directory anymore. See also: http://www.androidcentral.com/kitkat-sdcard-changes

file.mkdirs() not working

i want to write to a file in the sdcard of my phone.i used the below code to do this.
private CSVWriter _writer;
private File _directory;
public String _fileTestResult;
private String PATH_FILE_EXPORT = "/applications/foru/unittestframework/";
public ExportData(){
_writer=null;
_directory = new File(Environment.getExternalStorageDirectory () +PATH_FILE_EXPORT);
if(!_directory.exists())
_directory.mkdirs();
}
public void exportResult(String testcaseNum,String testcase,String status){
try {
if(_directory.exists()){
//do something
}
but mkdirs() is not working.so i could not excecute following code in the if condition.please help me.
note:i have given the permission in manifest file.
EDIT:
i am using this file write option for storing the result of automation testing using robotium.i have created a normal project and tried to create directory in sdcard.but the same code when i am using in this testproject it is not working.why like that?dont unit testing framework support this?
have you add the correct permission in your manifest ?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Edit : ok, i just read your note for permission.
If it's help you this is my sdcard cache code :
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
String evtDir = "";
if(evt > 0){
evtDir = File.separator + evt;
}
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory()
+ File.separator
+ "Android"
+ File.separator
+ "data"
+ File.separator
+ Application.getApplicationPackageName()
+ File.separator + "cache"
+ evtDir);
}else
cacheDir=context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
}
Try below code
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()) {
imagefolder = new File(root,
mycontext.getString(R.string.app_name));
imagefolder.mkdirs();
}
} catch (Exception e) {
Log.e("DEBUG", "Could not write file " + e.getMessage());
}
Try with:
if(!_directory.exists())
_directory.mkdir();
Also check this - Creating a directory in /sdcard fails

Categories

Resources