Estoy tratando de descargar un archivo mediante la libreria Fetch (https://github.com/tonyofrancis/Fetch) y que se guarde en la raiz de la tarjeta sd externa, pero cuando lo intento, me marca permiso denegado. he investigado que para escribir en la sd externa es necesario utilizar el framewrok de almacenamiento (https://developer.android.com/guide/topics/providers/document-provider?hl=es-419) pero no logro entender bien como utilizarlo, cabe mencionar que ya tengo los permisos de escritura en el manifest y en ejecución.
este es mi codigo:
Fetch fetch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this)
.setDownloadConcurrentLimit(3)
.build();
fetch = Fetch.Impl.getInstance(fetchConfiguration);
fetch.addListener(new FetchListener() {
@Override
public void onAdded(Download download) {
Toast.makeText(getApplicationContext(),"Added",Toast.LENGTH_SHORT).show();
}
@Override
public void onQueued(Download download, boolean b) {
Toast.makeText(getApplicationContext(),"Queued",Toast.LENGTH_SHORT).show();
}
@Override
public void onWaitingNetwork(Download download) {
}
@Override
public void onCompleted(Download download) {
Toast.makeText(getApplicationContext(),"Completed",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Download download, com.tonyodev.fetch2.Error error, Throwable throwable) {
Toast.makeText(getApplicationContext(),"Error "+error.getThrowable(),Toast.LENGTH_SHORT).show();
}
@Override
public void onDownloadBlockUpdated(Download download, DownloadBlock downloadBlock, int i) {
}
@Override
public void onStarted(Download download, List<? extends DownloadBlock> list, int i) {
Toast.makeText(getApplicationContext(),"Started",Toast.LENGTH_SHORT).show();
}
@Override
public void onProgress(Download download, long l, long l1) {
Toast.makeText(getApplicationContext(),"Progres "+l,Toast.LENGTH_SHORT).show();
}
@Override
public void onPaused(Download download) {
}
@Override
public void onResumed(Download download) {
}
@Override
public void onCancelled(Download download) {
}
@Override
public void onRemoved(Download download) {
}
@Override
public void onDeleted(Download download) {
}
});
solicitarPermiso();
}
private void descargar(){
String url = "https://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4";
String file = "/storage/sdcard/archivo.mp4";
request = new Request(url, file);
request.setPriority(Priority.HIGH);
request.setNetworkType(NetworkType.ALL);
request.addHeader("clientKey", "SD78DF93_3947&MVNGHE1WONG");
fetch.enqueue(request, updatedRequest -> {
Toast.makeText(getApplicationContext(),"Exito enqueue",Toast.LENGTH_SHORT).show();
}, error -> {
Toast.makeText(getApplicationContext(),"Error enqueue"+error.getThrowable(),Toast.LENGTH_SHORT).show();
});
}
public void solicitarPermiso(){
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}else {
descargar();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 1){
if (!(grantResults.length ==1 && grantResults[0] == PackageManager.PERMISSION_GRANTED)){
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(Main2Activity.this);
builder.setTitle("Aviso")
.setMessage("Es necesario el permiso de archivos para descargar")
.setPositiveButton("Dar Permiso", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
solicitarPermiso();
}
}).setCancelable(false)
.show();
}else {
descargar();
}
}
}
Log:
E/ERROR_TAG: Error enqueuejava.io.IOException: Permission denied