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 }