简体   繁体   中英

Download File with android download manager and save in application folder

How can I download some files from my web server in my android application and store them in application folder in root location or in a private folder in internal/external memory.

I can download files but i can't store them in private folder.

Already i write my download manger and i have some issues with showing notifications (like download percent) with it.

I'm not exactly sure of what you want to do with "private folder"... but this is how I download files with a progress callback:

public class Downloader extends Thread implements Runnable{
    private String url;
    private String path;
    private DownloaderCallback listener=null;

    public Downloader(String path, String url){
        this.path=path;
        this.url=url;
    }

    public void run(){
        try {
            URL url = new URL(this.url);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();

            String filename = urlConnection.getHeaderField("Content-Disposition");
            // your filename should be in this header... adapt the next line for your case
            filename = filename.substring(filename.indexOf("filename")+10, filename.length()-2);

            int total = urlConnection.getContentLength();
            int count;

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(path+"/"+filename);

            byte data[] = new byte[4096];
            long current = 0;

            while ((count = input.read(data)) != -1) {
                current += count;
                if(listener!=null){
                    listener.onProgress((int) ((current*100)/total));
                }
                output.write(data, 0, count);
            }

            output.flush();

            output.close();
            input.close();

            if(listener!=null){
                listener.onFinish();
            }
        } catch (Exception e) {
            if(listener!=null)
                listener.onError(e.getMessage());
        }
    }

    public void setDownloaderCallback(DownloaderCallback listener){
        this.listener=listener;
    }

    public interface DownloaderCallback{
        void onProgress(int progress);
        void onFinish();
        void onError(String message);
    }
}

To use it:

Downloader dl = new Downloader("/path/to/save/file", "http://server.com/download");
dl.setDownloaderCallback(new DownloaderCallback{
    @Override
    void onProgress(int progress){

    }

    @Override
    void onFinish(){

    }

    @Override
    void onError(String message){

    }
});
dl.start();

If you want your downloaded file in external storage, you can use.

String storagePath = Environment.getExternalStorageDirectory().getPath()+ "/Directory_name/";
//Log.d("Strorgae in view",""+storagePath);
File f = new File(storagePath);
if (!f.exists()) {
    f.mkdirs();
}
//storagePath.mkdirs();
String pathname = f.toString();
if (!f.exists()) {
    f.mkdirs();
}
//Log.d("Storage ",""+pathname);
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri uri = Uri.parse(image);
checkImage(uri.getLastPathSegment());
if (!downloaded) {
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir("/Directory_name", uri.getLastPathSegment());
    Long referese = dm.enqueue(request);
    Toast.makeText(getApplicationContext(), "Downloading...", Toast.LENGTH_SHORT).show();
}

for your private mode you will have to encrypt the file and store it on sd card /internal. refer this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM