So I am trying to store a PDF file on the user's android device by creating a Folder and then storing it. I am using getExternalStorageDir() but this has been deprecated under API29. The problem is Andriod guidelines say to opt-out out of scoped storage I have to put this in Manifest File
<manifest xmlns:android="http://schemas.android.com/apk/res/android
....
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:requestLegacyExternalStorage="true"
....
</application>
This is not working anymore. I can download on Devices with andriod M but Not able to in recent 9/10 devices.
public class FinalActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
private static final int WRITE_REQUEST_CODE = 300;
private static final String TAG = MainActivity.class.getSimpleName();
private String url;
SessionManagement sessionManagement;
String userID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
sessionManagement = new SessionManagement(this);
HashMap<String, String> user = sessionManagement.getUserDetail();
userID = user.get(sessionManagement.ID);
TextView submit = findViewById(R.id.download);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (EasyPermissions.hasPermissions(FinalActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Get the URL entered
url = F.url1 + userID + "/FPO.pdf";
new DownloadFile().execute(url.replaceAll(" ", "%20"));
} else {
//If permission is not present request for the same.
EasyPermissions.requestPermissions(FinalActivity.this, "This app needs access to your file storage so that it can write files.", WRITE_REQUEST_CODE, Manifest.permission.READ_EXTERNAL_STORAGE);
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, FinalActivity.this);
}
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
//Download the file once permission is granted
url = F.url1 + userID + "/FPO.pdf";
new DownloadFile().execute(url.replaceAll(" ", "%20"));
}
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
Log.d(TAG, "Permission has been denied");
}
private class DownloadFile extends AsyncTask<String, String, String> {
private ProgressDialog progressDialog;
private String fileName;
private String folder;
private boolean isDownloaded;
@Override
protected void onPreExecute() {
super.onPreExecute();
this.progressDialog = new ProgressDialog(FinalActivity.this);
this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.progressDialog.setCancelable(false);
this.progressDialog.show();
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// getting file length
int lengthOfFile = connection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
String timestamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
//Extract file name from URL
fileName = f_url[0].substring(f_url[0].lastIndexOf('/') + 1, f_url[0].length());
//External directory path to save fileb n
folder = Environment.getExternalStorageDirectory() + File.separator + "FPO/";
//Create LSK folder if it does not exist
File directory = new File(folder);
if (!directory.exists()) {
directory.mkdirs();
}
// Output stream to write file
OutputStream output = new FileOutputStream(folder + fileName.replaceAll("%20", " "));
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lengthOfFile));
Log.d(TAG, "Progress: " + (int) ((total * 100) / lengthOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
return "Downloaded at: " + folder + fileName.replaceAll("%20", " ");
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
Log.i("error123", e.getMessage());
return e.getMessage();
}
// return "Something went wrong";
}
protected void onProgressUpdate(String... progress) {
// setting progress percentage
progressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String message) {
// dismiss the dialog after the file was downloaded
this.progressDialog.dismiss();
Intent intent = new Intent(FinalActivity.this, Welcome_screen1.class);
startActivity(intent);
// Display File path after downloading
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
}
}