Android mkdir not making folder

孤街浪徒 提交于 2019-11-27 17:15:28

问题


Tonight I am currently having issues doing something that I thought would be simple... making a folder in /mnt/sdcard.

I have set the follow permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

My Main.java has the following to make the folder:

public class Main extends TabActivity {
    static int index = 1;
    private static final String TAG = "Main";       

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        File folder = new File(Environment.getExternalStorageDirectory () + "/tallgrass/images");
        boolean success = false;
        if(!folder.exists()){
            success = folder.mkdir();
        }
        if (!success){ 
            Log.d(TAG,"Folder not created.");
        }
        else{
            Log.d(TAG,"Folder created!");
        }
    }

I get the "Folder created!" message in my log but when I check both /mnt/sdcard and /sdcard neither one has the folder. I have tried calling:

Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())

and it returns true. I just can't figure this one out because all signs are pointing that it should work. I have also tried it with the phone disconnected from the PC in case the SD card was mounting or something as I am currently using my phone instead of the emulator for developing. Speaking of which, does debuggable to true maybe prevent it from making the folder?

Thanks!


回答1:


Does the /mnt/sdcard/tallgrass/ directory exist? (I'm guessing not, but you never know.)

The File.mkdirs() method will create all needed directories; mkdir() will only create the last directory in the pathname.




回答2:


Check where you are putting the permissions they must go in this way:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="edu.una.info.app">
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.CALL_PHONE" />

<application android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme">

    <activity
        android:name="edu.una.info.SplashScreenActivity"


来源:https://stackoverflow.com/questions/5958600/android-mkdir-not-making-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!