Текст ошибки IOException
"storage/emulated/0/Download/111.txt" (Permission Denied)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isExternalStorageMounted() && !isExternalStorageReadOnly()){
OutputStream outputStream;
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
File f = new File(dir, "111.txt");
try {
outputStream = new FileOutputStream(f);
outputStream.close();
Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
public static boolean isExternalStorageMounted() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}
public static boolean isExternalStorageReadOnly() {
return Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState());
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Это тоже добавлял
android:requestLegacyExternalStorage="true"
но у меня версия не выше 29, да и не работает всё равно.
Версия 9, Api 28