Reputation: 153
i have create a modul to download file from server to my android apps like this
@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());
//Append timestamp to file name
fileName = timestamp + "_" + fileName;
//External directory path to save file
folder = Environment.getExternalStorageDirectory() + File.separator + "simpel/";
//Create androiddeft 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);
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;
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return "Something went wrong";
}
but when i try to download, i always get error like this
E/Error:: /storage/emulated/0/simpel/2019.07.06.18.57.51_REGISTER_TILANG.xlsx (Permission denied)
even though I have added this to my manifest
android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE
i use this libary:
implementation 'pub.devrel:easypermissions:0.2.0'
Upvotes: 2
Views: 3303
Reputation: 427
I know it's been more than a year but I hope this helps someone.
I worked with your code now and I found out the mistake you might have made.
You might have added these android.permission.READ_EXTERNAL_STORAGE android.permission.WRITE_EXTERNAL_STORAGE
to your manifest.xml
but you need to ask for the permission in your code.
Even using the library (same I used) implementation 'pub.devrel:easypermissions:0.2.0'
it seems you didn't check for both READ and WRITE permissions (something like this):
private static final int WRITE_REQUEST_CODE = 1;
if (EasyPermissions.hasPermissions(DownloadActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//your asyncTask to download
downloadFile.execute(url);
} else {
//If permission has no be granted, request
EasyPermissions.requestPermissions(DownloadActivity.this, "This app needs access to your file storage" , WRITE_REQUEST_CODE, Manifest.permission.READ_EXTERNAL_STORAGE);
}
Notice that in the code above, only WRITE permission was checked. If there isn't, then READ permission was requested (which is kind of contradictory). The correct would be checking for both permissions then if either of them has not been granted, the system will request for thr two. Compare the following code:
private static final int REQUEST_CODE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
if (EasyPermissions.hasPermissions(DownloadActivity.this, PERMISSIONS_STORAGE)) {
//your asyncTask to download
downloadFile.execute(url);
} else {
//If permission has not been granted, request
EasyPermissions.requestPermissions(DownloadActivity.this, "This app needs access to your file storage", REQUEST_CODE, PERMISSIONS_STORAGE);
}
I had similarcode as yours and had the same error, I fixed it this way.
Upvotes: 1