Unable to create folder in / mnt / sdcard

I think I'm doing everything right to create a folder that I can write later in my application, but it does not seem to work. Any tips on how I failed?

It is trying correctly to create / mnt / sdcard / gradebook

I have a use - permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" in my manifest. I work on a real device (Samsung Galaxy S) and I do not use sdcard as storage when it is connected to my computer.

boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media mExternalStorageAvailable = true; mExternalStorageWriteable = false; Toast.makeText(this,"This Application needs a writable external storage (sdcard).",Toast.LENGTH_SHORT).show(); finish(); } else { // Something else is wrong. It may be one of many other states, but all we need // to know is we can neither read nor write mExternalStorageAvailable = mExternalStorageWriteable = false; Toast.makeText(this,"This Application needs a mounted external storage (sdcard).",Toast.LENGTH_SHORT).show(); finish(); } File folder = new File(Environment.getExternalStorageDirectory () + "/gradeBook"); boolean success = false; if(!folder.exists()){ success = folder.mkdir(); } if (!success) { Log.e("FILE", "can't create " + folder); } else { Log.i("FILE", "directory is created"); } 

Here is the manifest

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ulsanonline.gradebook" android:versionName="@string/version" android:installLocation="auto" android:versionCode="2"> <uses-sdk android:minSdkVersion="8"></uses-sdk> <application android:icon="@drawable/icon" android:debuggable="true" android:persistent="false" android:hasCode="true" android:minSdkVersion="8"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> <activity android:name=".CourseWork" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 
+4
source share
2 answers

uses-permission should be placed directly as <manifest> child, so it should be:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ulsanonline.gradebook" android:versionName="@string/version" android:installLocation="auto" android:versionCode="2"> <uses-sdk android:minSdkVersion="8"></uses-sdk> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> <application android:icon="@drawable/icon" android:debuggable="true" android:persistent="false" 
+8
source

Try it,

 //Declare a string String NewFolder = "/myNewFolder"; String extStorageDirectory; //external storage directory path extStorageDirectory = Environment.getExternalStorageDirectory().toString(); //creating a new folder File mkTarDir=new File(extStorageDirectory + NewFolder); mkTarDir.mkdir(); //path to save a image File file = new File(extStorageDirectory + NewFolder ,"myimage"+String.valueOf(System.currentTimeMillis()) + ".PNG"); 
0
source

All Articles