Cannot create a folder and put a file inside this folder

AndreaF

I want to create a folder and put all generated file in this folder so I have created this method to create a directory in external storage named MyAppFolder and put a .nomedia file in this folder to avoid media indexing

static String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
static String baseAppDir = "MyAppFolder";
static String fileHider = ".nomedia";

 public static void createFolder() {
        try {

            File mainDirectory = new File(baseDir + File.separator + baseAppDir);
            if (!(mainDirectory.exists())) {
                mainDirectory.mkdirs();
                File outputFile = new File(mainDirectory, fileHider);
                try {
                    FileOutputStream fos = new FileOutputStream(outputFile);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception exc) {
            System.out.println("ERROR: " + exc.toString());
            exc.printStackTrace();
        }
    }

I'm testing this on emulator but doesn't work, and I cannot understand how should I fix it.

The error log is:

java.io.FileNotFoundException: /storage/sdcard/MyAppFolder/.nomedia: open failed: ENOENT (No such file or directory)
    02-12 20:11:51.758    4899-4899/com.myapp.testapp W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
    02-12 20:11:51.758    4899-4899/com.myapp.testapp W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
    02-12 20:11:51.758    4899-4899/com.myapp.testapp W/System.err﹕ at com.myapp.testapp.MyFileManager.createFolder(MyFileManager.java:272)

I have also tried with

  File outputFile = new File(mainDirectory, fileHider);
  if(!outputFile.exists()) {
       outputFile.createNewFile();
  }
  try {
       FileOutputStream fos = new FileOutputStream(outputFile, false);
  } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
       e.printStackTrace();
  }

same result

yushulx

Make sure you have the permission

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

Your code can work on my device. If you do have the permission, due to some tiny differences between our Android systems, you can try to create a hidden directory, and create a file inside of it.

static String baseAppDir = ".MyAppFolder";
static String fileHider = "nomedia";

Type in "ls -a" to check whether the hidden file has been really created. Don't 100% trust the exception log sometimes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

create zip file inside a folder but without folder inside that zip

From Dev

SharePoint - create folder inside an existing folder (at server)

From Dev

Cannot create a folder because such folder already exists?

From Dev

Create folder / file structure

From Dev

create folder and file in Powershell

From Dev

Create file/folder list

From Dev

create folder and file in Powershell

From Dev

Create file on unmounted folder

From Dev

Create a file folder for app

From Dev

Cannot delete folder or files inside

From Dev

Swift put dowloaded file into folder

From Dev

Android create multiple file inside a folder in internal storage

From Dev

Batch files inside folder to create MediaInfo.nfo file

From Dev

Cannot create empty folder in gradle

From Dev

PHP File count inside a folder

From Dev

.gitignore not working for file inside folder

From Dev

Folder size inside a compressed file

From Dev

PHP File count inside a folder

From Dev

put libraries inside exe folder in Linux

From Dev

Create a folder for each file in a folder without the folder itself

From Dev

Cannot access a file or folder in ubuntu

From Dev

cannot move a file into other folder

From Dev

text file cannot be made a folder

From Dev

Creating a text file inside a folder then rename folder to match a filename inside it

From Dev

Ionic Zip cannot zip folder inside folder C#

From Dev

Create a html file and save in a folder

From Dev

Create xls file and save to a folder

From Dev

Create file in folder: permission denied

From Dev

Create a variable based on file in folder