ваше использование ftech
implementation "com.tonyodev.fetch2:fetch2:3.0.10"
использование Androidx:
implementation "androidx.tonyodev.fetch2:xfetch2:3.1.4"
загрузка любого файла
if (isStoragePermissionGranted()) {
fetch = Fetch.Impl.getInstance(fetchConfiguration);
String url = nameFileDownload;
String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + nameFileDownload.substring(url.lastIndexOf('/') + 1);
;
if (fileExist(file)) {
view(null, nameFileDownload.substring(url.lastIndexOf('/') + 1));
} else {
progress_download.setVisibility(View.VISIBLE);
btn_download.setVisibility(View.GONE);
final com.tonyodev.fetch2.Request request = new com.tonyodev.fetch2.Request(url, file);
request.setPriority(Priority.NORMAL);
request.setNetworkType(NetworkType.ALL);
fetch.enqueue(request, updatedRequest -> {
fetch.addListener(fetchListener);
//Request was successfully enqueued for download.
}, error -> {
progress_download.setVisibility(View.GONE);
btn_download.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, "feild", Toast.LENGTH_SHORT).show();
//An error occurred enqueuing the request.
});
}
} else {
Toast.makeText(MainActivity.this, "perssion denid", Toast.LENGTH_SHORT).show();
}
}
isStoragePermissionGranted:
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v("TAG", "Permission is granted");
return true;
} else {
Log.v("TAG", "Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
Log.v("TAG", "Permission is granted");
return true;
}
}
манифесты:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>