Reputation: 21
I would like to create a "RawMeasurement" directory in my phone in the same place as Music, Document, Picture... under Android R
I tested with lower api versions, it works, but under android R, impossible.
I think the problem comes from Environement.getExternalStorageDirectory()
I've been looking for other methods, but without results.
Ideas? thank you in advance
public void save(){
String state;
state = Environment.getExternalStorageState();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm", Locale.getDefault());
String currentDateAndTime = sdf.format(new Date());
if(Environment.MEDIA_MOUNTED.equals(state)){
File Dir = new File(Environment.getExternalStorageDirectory().getPath(),"RawMeasurement");
if(!Dir.exists()) {
Dir.mkdirs();
Toast.makeText(getContext(),"new folder",Toast.LENGTH_LONG).show();
}
File file = new File(Dir,"RawMeasurements"+currentDateAndTime+".txt");
String message = "bla bla bla " ;
try{
FileOutputStream out = new FileOutputStream(file);
out.write(message.getBytes());
out.close();
Toast.makeText(getContext(),"message saved",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
logError("error");
} catch (IOException e) {
e.printStackTrace();
}
}else{
Toast.makeText(getContext(),"echec",Toast.LENGTH_LONG).show();
}
}
Upvotes: 2
Views: 7054
Reputation: 55
Create Folder in Android 11+
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/Create Your Folder");
file.mkdir();
Here is the Two Methods
private boolean isPermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
return ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED;
}
}
private void askPermissionForStorage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
startActivityForResult(intent, 2);
} catch (Exception e) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(intent, 2);
}
} else {
ActivityCompat.requestPermissions(SplashScreen.this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
}, 1);
}
}
Upvotes: 1
Reputation: 1881
Apps cannot create their own app-specific directory on external storage in Android 11. You can create folders inside public directories. For example in Documents folder.
final File dir;
if (Build.VERSION_CODES.R > Build.VERSION.SDK_INT) {
dir = new File(Environment.getExternalStorageDirectory().getPath()
+ "//MyApp");
} else {
dir = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS).getPath()
+ "//MyApp");
}
if (!dir.exists())
dir.mkdir();
Upvotes: 2
Reputation: 664
The permission system since android R has changed a bit, see Storage updates in Android 11 in official documentation for more information.
I used externalMediaDirs.first()
for my own project but this will place the files in Android/Media/com.example/
Upvotes: 1
Reputation: 1006614
I would like to create a "RawMeasurement" directory in my phone in the same place as Music, Document, Picture... under Android R
Sorry, but that is not really supported. You can request the MANAGE_EXTERNAL_STORAGE
permission, at which point you could do what you want. My guess is that Google will ban your app from the Play Store unless you can provide a really good justification for that permission.
What Google wants you to do is use ACTION_OPEN_DOCUMENT_TREE
to let the user decide where on the user's device that your app should put the user's raw measurements. You can then create a sub-tree (directory) under the user-chosen location, and you can put your content into that sub-tree.
Upvotes: 1