0

My project storage directory : /storage/emulated/0/Android/data/myprojectapp

issue : am to get the storage access of my project directory in the runtime

Added below mentioned permission in the AndroidManifest.xml

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

Code to check for the runtime permission

@Override
    public void onCreate(Bundle savedInstanceState){
        Log.d(TAG, "onCreate() [IN]");
        super.onCreate(savedInstanceState);
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    PERMISSION_READ_EXTERNAL_STORAGE_CONSTANT);
        } else {
            setMainActivity();
        }
        
    }

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_READ_EXTERNAL_STORAGE_CONSTANT: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 &&
                        grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "permission granted"); // able to see this is the log as permission granted
                    setMainActivity();
                } else {
                    Toast.makeText(this, "Need sdcard permissions", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

//After the permssion is granted in runtime below mentioned method is called
 private void setMainActivity() {
    static final String PKGPUSH_DIR = "/storage/emulated/0/Android/data/myprojectapp";
   //checking whether the  is writable or not
        File directory = new File(Constants.PKGPUSH_DIR);
        if(directory.canWrite()){
            Log.i(TAG, "set as writable "+directory);
        }else{
            Log.i(TAG, "Set as not writable"); // always it return the log as set as not writable
        }  


 }

first of all directory is not created after give the runtime permission Could someone tell me why the directory is not created even after giver the runtime permission and why it is not accesioble?

is it because of the Android 11 scoped storage change :

defaultConfig { applicationId "myprojectapp" minSdkVersion 26 targetSdkVersion 30 }

  • First, `WRITE_EXTERNAL_STORAGE` will not give you write access to arbitrary locations on external storage on Android 11 and higher. Second, your hard-coded path may be wrong for your device. **ALWAYS** use a method to get root paths -- it looks like you are aiming to use `Environment.getExternalStorageDirectory()`. – CommonsWare Mar 14 '21 at 11:43
  • How about run time permissions ? – TraderAndroid Mar 14 '21 at 11:59
  • `WRITE_EXTERNAL_STORAGE` needs to be requested at runtime. You are only asking for `READ_EXTERNAL_STORAGE`. You should change `new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}` to be `new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}`. However, you still should not be able to create and write to your desired directory. You might want to consider using `ACTION_CREATE_DOCUMENT` from the Storage Access Framework and let the *user* decide where on the *user's* device (or the *user's* cloud storage) to put the *user's* content. – CommonsWare Mar 14 '21 at 12:25
  • is Permission WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE wouldn't help me to create/read the files inside the app specific storage ?... – TraderAndroid Mar 16 '21 at 06:50
  • You do not need those permissions for what the SDK documentation refers to as "app specific storage", such as `getFilesDir()` and `getExternalFilesDir()` on `Context`. The directory that you are trying to use might be related to those -- I cannot tell. But this is one of the reasons why you should **NEVER** hardcode paths. Always use methods on objects, like `Context`, to get the base directories to use. – CommonsWare Mar 16 '21 at 11:32

0 Answers0