The following code output is "Write granted" but after that the error it '/storage/emulated/0/Download/test.txt: open failed: EACCES (Permission denied)'
What's wrong?
@RunWith(AndroidJUnit4.class)
public class FileStorageTest {
private static final String TAG = FileStorageTest.class.getSimpleName();
@Test
public void test() throws Exception {
Context context = InstrumentationRegistry.getTargetContext();
if (context.checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Write granted");
} else {
Log.e(TAG, "Write refused");
}
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"test.txt");
new FileOutputStream(file).close();
}
}