Android Open Source - Broadsheet.ie-Android Download File Request






From Project

Back to project page Broadsheet.ie-Android.

License

The source code is released under:

Copyright (c) 2013 Karl Monaghan (http://karlmonaghan.com/) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Soft...

If you think the Android project Broadsheet.ie-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ie.broadsheet.app.requests;
//from   w  w w.  j a va  2  s. c o m
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import android.net.Uri;

import com.octo.android.robospice.request.SpiceRequest;

public class DownloadFileRequest extends SpiceRequest<File> {
    private static final int MAX_BUFFER_SIZE = 1048576;

    private String source;

    private File destinationFolder;

    public DownloadFileRequest(String source, File destinationFolder) {
        super(File.class);
        this.source = source;
        this.destinationFolder = destinationFolder;
    }

    @Override
    public File loadDataFromNetwork() throws IOException {
        int bytesRead;
        URL url = new URL(source);

        File destFile = new File(destinationFolder, Uri.parse(source).getLastPathSegment());
        FileOutputStream fileOutput = new FileOutputStream(destFile);

        InputStream inputStream = (InputStream) url.getContent();
        byte[] buffer = new byte[MAX_BUFFER_SIZE];
        while ((bytesRead = inputStream.read(buffer)) > 0) {
            fileOutput.write(buffer, 0, bytesRead);
        }
        fileOutput.close();

        return destFile;
    }

    @Override
    public String toString() {
        return "DownloadFileRequest [source=" + source + ", destinationFolder=" + destinationFolder + "]";
    }
}




Java Source Code List

ie.broadsheet.app.BaseFragmentActivity.java
ie.broadsheet.app.BroadsheetApplication.java
ie.broadsheet.app.CommentListActivity.java
ie.broadsheet.app.PostDetailActivity.java
ie.broadsheet.app.PostListActivity.java
ie.broadsheet.app.adapters.CommentAdapter.java
ie.broadsheet.app.adapters.PostListAdapter.java
ie.broadsheet.app.adapters.PostListEndlessAdapter.java
ie.broadsheet.app.client.http.MultipartFormDataContent.java
ie.broadsheet.app.dialog.AboutDialog.java
ie.broadsheet.app.dialog.MakeCommentDialog.java
ie.broadsheet.app.dialog.TipDialog.java
ie.broadsheet.app.dialog.WebViewDialog.java
ie.broadsheet.app.fragments.PostDetailFragment.java
ie.broadsheet.app.fragments.PostListFragment.java
ie.broadsheet.app.model.json.AttachmentItem.java
ie.broadsheet.app.model.json.Attachments.java
ie.broadsheet.app.model.json.Author.java
ie.broadsheet.app.model.json.Category.java
ie.broadsheet.app.model.json.Comment.java
ie.broadsheet.app.model.json.Image.java
ie.broadsheet.app.model.json.PostList.java
ie.broadsheet.app.model.json.Post.java
ie.broadsheet.app.model.json.ResponseData.java
ie.broadsheet.app.model.json.SinglePost.java
ie.broadsheet.app.model.json.SubmitTipResponse.java
ie.broadsheet.app.model.json.Tag.java
ie.broadsheet.app.requests.DownloadFileRequest.java
ie.broadsheet.app.requests.MakeCommentRequest.java
ie.broadsheet.app.requests.PostListRequest.java
ie.broadsheet.app.requests.PostRequest.java
ie.broadsheet.app.requests.SubmitTipRequest.java
ie.broadsheet.app.services.BroadsheetServices.java