Search code examples
javaandroidinternal-storage

Can't create folder in internal storage


Well, I'm trying to create a folder in my internal storage. I have watched some tutorial but it's not working at all.

        
private void createDir() {
String folderName;


        folderName = "myFolder";
        File file =  new File(Environment.getExternalStorageDirectory(), folderName);
        if(!file.exists()){
            file.mkdir();
            Toast.makeText(getContext(),"Successful", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getContext(),"Folder already exist", Toast.LENGTH_SHORT).show();
        }
}

This is my method to create the new directory. When I launch it, I receive the Toast "Successful" all the time. But the directory is never created.

Just below the code for the permissions.

                    if(!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.SEND_SMS)){
                        String[] permissions = {Manifest.permission.WRITE_CALL_LOG,Manifest.permission.SEND_SMS,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.WRITE_CONTACTS,Manifest.permission.READ_SMS};
                        ActivityCompat.requestPermissions(getActivity(),permissions,1);
                    }else{
                        lay_dataset1=view.findViewById(R.id.lay_dataset1); 
                        messagePerm();
                    }

Here my manifest :

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

Can someone explain what is happening :)

EDIT :

 private void copyAssets() {
        AssetManager assetManager = getContext().getAssets();

        String[] files = null;
        try {
            files = assetManager.list("");
        } catch (IOException e) {
            Log.e("tag", "Failed to get asset file list.", e);
        }
        for(String filename : files) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(filename);
                File outFile = new File(getContext().getExternalFilesDir(null).getParent().replace("files","myfolder"), filename);
                out = new FileOutputStream(outFile);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } catch(IOException e) {
                Log.e("tag", "Failed to copy asset file: " + filename, e);
            }
        }
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }

I tried this, I was able to move files that were in my "asset" folder at the same level as the "files" directory so why shouldn't I have the right to create a folder in this same location ?


Solution

  • At the first time you run your application, the app external storage directory at Android/data/<package.name>/files is not created until you call this method getExternalFilesDir(null) Twice.

    So try this code..

    //Essential for creating the external storage directory for the first launch
    getExternalFilesDir(null); 
    
    /*
     output->> /storage/emulated/0/Android/data/<package.name>/files 
    */
    Log.i("HINT",getExternalFilesDir("").getAbsolutePath());
    
    
    //Or create your custom folder
    File outFile = new File(getExternalFilesDir(null).getParent(),"myfolder");
    //make it as it is not exists
    outFile.mkdirs();
    
    /*
     output->> /storage/emulated/0/Android/data/<package.name>/myfolder
    */
    Log.i("HINT",outFile.getAbsolutePath());