0

I am creating folder in SD card I tried many code in stackoverflow but its not working help me to solve this I am using Pixel 2 API 29 Emulator Compiled version 29 minSdkVersion 26 targetSdkVersion 29 and I added Permission external storage permission and its shown getExternalStorageDirectory() depreciated
I am tried codes

File mydir = new File(Environment.getExternalStorageDirectory() + "/mydir/");
if(!mydir.exists())
    mydir.mkdirs();
else
    Log.d("error", "dir. already exists");

Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
Ajith
  • 15
  • 5

1 Answers1

0

Use comma instead of +(sign) and try this code.

Try this code:

File mydir = new File(Environment.getExternalStorageDirectory(), "/mydir/");

    if (!mydir.exists()) {
        mydir.mkdirs();
        Log.e("directory", "folder Created");
    } else {
        Log.e("directory", "folder already exists");
    }

Also Cross verify your permission Code with below snippets:

  @TargetApi(Build.VERSION_CODES.M)
public void checkMyPermission() {
    String[] permissionArrays = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(permissionArrays, 11111);
    } else {
        Toast.makeText(mcontext, "Permission Granted", Toast.LENGTH_SHORT).show();
    }
}


@TargetApi(Build.VERSION_CODES.M)
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    boolean openActivityOnce = true;
    boolean openDialogOnce = true;
    if (requestCode == 11111) {
        boolean isPermitted = false;
        for (int i = 0; i < grantResults.length; i++) {
            String permission = permissions[i];

            isPermitted = grantResults[i] == PackageManager.PERMISSION_GRANTED;

            if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                // user rejected the permission
                boolean showRationale = shouldShowRequestPermissionRationale(permission);
                if (!showRationale) {
                    //execute when 'never Ask Again' tick and permission dialog not show
                } else {
                    if (openDialogOnce) {
                        alertView();
                    }
                }
            }
        }

    }
}

private void alertView() {
    AlertDialog.Builder dialog = new AlertDialog.Builder(mcontext);

    dialog.setTitle("Permission Denied")
            .setInverseBackgroundForced(true)
            //.setIcon(R.drawable.ic_info_black_24dp)
            .setMessage("Without those permission the app is unable to save your profile. App needs to save profile image in your external storage and also need to get profile image from camera or external storage.Are you sure you want to deny this permission?")

            .setNegativeButton("I'M SURE", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialoginterface, int i) {
                    dialoginterface.dismiss();
                }
            })
            .setPositiveButton("RE-TRY", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialoginterface, int i) {
                    dialoginterface.dismiss();
                    checkMyPermission();

                }
            }).show();
}
Nimesh Patel
  • 1,394
  • 13
  • 26