1

I created a some lines in order to create a directory on the mainActivity class

// Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_CONTACTS);
                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }

And

        File tSubLogDirectory = new File(Environment.getExternalStorageDirectory()+File.separator+"data");
        if(!tSubLogDirectory.exists())
        tSubLogDirectory.mkdirs();
        if(tSubLogDirectory.exists())
            System.out.println("Created ");

set permission lines in the android manifest file in order to allow writing/reading files on the external storage

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ny_project.blue">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application...

but still the output I get indicates that the directory did not created

I/System.out: ***********************************************************
I/System.out: /storage/emulated/0/data
I/System.out: ***********************************************************

I have also looked in storage/emulated/0/data directory in order to see if by mistake the file is in, but no, it is not.

can someone tell me please what I am doing wrong ?

amnon
  • 97
  • 1
  • 8

1 Answers1

0

project site https://github.com/Karumi/Dexter

incase you are offline https://mvnrepository.com/artifact/com.karumi/dexter for jar/aar file

Include the library in your build.gradle in app

dependencies{
implementation 'com.karumi:dexter:6.1.0'
}

or

for offline in android studio right click on app folder of the project, click New from the sub menu click on Module and scroll the slide to bottom and select import JAR/AAR Package and click next.

finaly browse and select the downloaded jar/aar, ok then click finish button.

wait untill it finish build.

after that type the below code remember the name must be the same.

dependencies{
implementation project(':dexter-6.1.0')
}

in MainActivity.java type

import android.Manifest;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionRequest;
import 
com.karumi.dexter.listener.multi.
MultiplePermissionsListener;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    askStorage();


 }


 public void createFolder(){

 File tSunLogDirectory = new 
 File(Environment.getExternalStorageDirectory()+
File.separator+"data");
if(!tSunLogDirectory.exists()){
    tSunLogDirectory.mkdir();
    Toast.makeText(MainActivity.this, "Folder created", 
Toast.LENGTH_LONG).show();
 }else{
    Toast.makeText(MainActivity.this, "Folder already exist 
 ", Toast.LENGTH_LONG).show();
  }

  }
 public void askStorage(){


    Dexter.withActivity(this)
            .withPermissions(

 Manifest.permission.READ_EXTERNAL_STORAGE,

 Manifest.permission.WRITE_EXTERNAL_STORAGE
                    )
            .withListener(new MultiplePermissionsListener() {
                @Override
                public void 
  onPermissionsChecked(MultiplePermissionsReport 
  report) {
                    // check if all permissions are granted
                    if (report.areAllPermissionsGranted()) {
                        // do you work now
                        createFolder();
                    }
                    // check for permanent denial of any 
 permission
                    if 
 (report.isAnyPermissionPermanentlyDenied()) {
                        // permission is denied

                    }
                }

                @Override
                public void 
 onPermissionRationaleShouldBeShown(
 List<PermissionRequest> list, PermissionToken 
 permissionToken) {

 permissionToken.continuePermissionRequest();
                }


            })
            .check();

    }
  }

check the project site for Single and Multiple permissions.

and also dont forget to include

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