Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DownloadManager not download file if it was already downloaded

I am using the DownloadManager to download my App-Files. If I put an url a second time into the DownloadManager it downloads the file and puts a -1 filename-1.file at the end. Is there a way to just not let the DownloadManager download it again? Or do I have to check that by myself?

Code:

private void downloadImages(final List<SomeClass> data) {
    RuntimeExceptionDao<SomeClass, Integer> someDao = DatabaseAdapter.getInstance().getSomeDao();
    DownloadManager downloadmanager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
    for(SomeClass someClass : data){
        DownloadManager.Request request = getRequest(someClass);
        someClass.mDownloadId = downloadmanager.enqueue(request);
        someDao.createOrUpdate(someClass);
    }
}

private DownloadManager.Request getRequest(SomeClass someClass) {
    Uri uri = Uri.parse(someClass.mImage);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
    request.setVisibleInDownloadsUi(false);
    request.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, car.getFileName());
    return request;
}
like image 757
Informatic0re Avatar asked Nov 02 '22 01:11

Informatic0re


1 Answers

This is how i solve it, you have to make a query to the download manager and verify if there is already a download with an equal title. if there isn't any coincidence then i create a file and use the exist function to verify if it is already on the download directory. if it is not there i start the download.

downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);

gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            //Crear objeto file con la ruta
            File ExistingFile =  new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS) + "/" + Archivos.get(position).getNombre());

            //Checar el downloadManager
            Cursor cursor = downloadManager.query( new Query() ); 
            boolean IsInDownloadManager;

            IsInDownloadManager = false;
            for (int i = 0; i < cursor.getCount() ; i++)
            {
                cursor.moveToPosition(i);
                Log.i("Click Grid", "Objetos en download manager [" + String.valueOf(i) + "] " + cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE)));
                if (Archivos.get(position).getNombre().equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE)))){                                          
                    IsInDownloadManager = true;
                    Log.i("Click Grid", "Objeto está en download Manager " + Archivos.get(position).getNombre());
                    break;
                }
            }

            if (IsInDownloadManager){
                //cursor esta aputando a la fila donde se quedó en el ciclo for
                int Status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                Log.i("Click Grid", cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)));

                if (Status == DownloadManager.STATUS_SUCCESSFUL){
                    Toast.makeText(getActivity() ,"Abriendo " +  Archivos.get(position).getNombre(), Toast.LENGTH_SHORT).show(); 
                    try { openFile(getActivity(),ExistingFile ); } catch (IOException e) {e.printStackTrace();}
                }else{
                     Toast.makeText(getActivity() ,Archivos.get(position).getNombre() + " ya se está descargando", Toast.LENGTH_SHORT).show();    
                }

            }else{

                if( ExistingFile.exists() ){
                    Toast.makeText(getActivity() ,"Abriendo " +  Archivos.get(position).getNombre(), Toast.LENGTH_SHORT).show();    
                    try { openFile(getActivity(),ExistingFile ); } catch (IOException e) {e.printStackTrace();}
                }else{
                    DescargarArchivo( Archivos.get(position) );
                }
            }

        }});
like image 149
Javobal Avatar answered Nov 12 '22 11:11

Javobal