Reputation: 4123
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.
Upvotes: 6
Views: 9893
Reputation: 1378
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();
}
Upvotes: 0
Reputation: 2962
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();
Upvotes: 3
Reputation: 2985
for your private mode you will have to encrypt the file and store it on sd card /internal. refer this
Upvotes: 0