Skip to content Skip to sidebar Skip to footer

How To Create Folder In External Storage And Check For Permission?

I want to create a folder on click of photo's layout. And on click of layout new folder created should get open if it is empty it should show the toast or it should get open with

Solution 1:

Try this,

Stringpath= Environment.getExternalStorageDirectory().toString();
Filedir=newFile(path,"/MeaVita");
 if (!dir.isDirectory()) {
             dir.mkdirs();
          }

add following line your manifest.xml

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

this may helps you.

EDIT 1:

It seems you are access empty/null File[] . you may modify the code structure like,

Stringpath= Environment.getExternalStorageDirectory().toString();
 Filedir=newFile(path,"/MeaVita");

 photosLayout.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {

        checkFilePermissions();
          if (!dir.isDirectory()) {
             dir.mkdirs();
          }

        Filefile1=newFile(Environment.getExternalStorageDirectory().getPath() + "/MeaVita");
        File[] listFile = file1.listFiles();

          if (listFile.length == 0) {
                Toast.makeText(MainActivity.this, "The folder is empty.", Toast.LENGTH_SHORT).show();
            } else {

                newMainActivity.SingleMediaScanner(MainActivity.this, listFile[0]);
            }

    }
});

Solution 2:

Stringpath= Environment.getExternalStorageDirectory().toString();
Filedir=newFile(path,"/MeaVita");

photosLayout.setOnClickListener(newView.OnClickListener() {

    @OverridepublicvoidonClick(View v) {

 if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
     if (!dir.isDirectory()) {
         dir.mkdirs();
     }


     if (listFile.length == 0) {
                Toast.makeText(MainActivity.this, "The folder is empty.",Toast.LENGTH_SHORT).show();
            } else {
                newMainActivity.SingleMediaScanner(MainActivity.this, listFile[0]);
            }
    }
  } else {
        checkFilePermissions();

} });

Post a Comment for "How To Create Folder In External Storage And Check For Permission?"