Using an emulated version of the Google Pixel 5 running Android 11 API 32, I'm attempting to make a program that will move files between folders at the press of a button, as a proof of concept I have made two folders on my device, Left (/storage/emulated/0/Left/)(Variable name: inputPath) and Right. (/storage/emulated/0/Right/)(Variable name: outputPath)
Inside "Left" I have stored a blank txt document named FILE.txt (/storage/emulated/0/Left/file.txt)(Variable name: inputFile) I am then attempting to move the file between folders with the code
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
Log.i("checking the value of mkdirs", String.valueOf(dir.mkdirs()));
}
in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file
out.flush();
out.close();
out = null;
// delete the original file
new File(inputPath + inputFile).delete();
Within my AndroidManifest I have the code
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
android:requestLegacyExternalStorage="true"
...
</application>
Then within my onCreate I call the method
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
Which requests file access the first time the user opens up the app, however, even if the user grants permissions I still get the error
open failed: EACCES (Permission denied)
I've looked up relevant solutions online but those I have looked at are not applicable to android 11, please help.