Back to project page LCFR-Mobile-Android.
The source code is released under:
Apache License
If you think the Android project LCFR-Mobile-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.
package org.dizon.lcfrmobile; //from ww w . j a va2 s . com import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.util.Calendar; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3Client; import android.content.Context; import android.os.AsyncTask; public class GetS3File extends AsyncTask<String, Integer, String>{ private Context context; private OnDownloadNotify notify; private String fileName; public GetS3File(Context _context) { context=_context; } public GetS3File(Context _context, OnDownloadNotify _notify) { context=_context; notify=_notify; } @Override protected String doInBackground(String... params) { fileName = params[0]; doDownload(fileName); return null; } public void doDownload(String fileName){ AWSCredentials creds = new BasicAWSCredentials(context.getResources().getString(R.string.aws_access_key), context.getResources().getString(R.string.aws_secret_key)); AmazonS3Client s3client = new AmazonS3Client(creds); Calendar c = Calendar.getInstance(); c.add(Calendar.MINUTE, 60); URL downloadURL = s3client.generatePresignedUrl(context.getResources().getString(R.string.aws_s3_bucket_name), fileName, c.getTime()); HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(downloadURL.toString()); HttpResponse response = null; try { response = client.execute(request); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return; } // Get the response BufferedInputStream bis = null; try { bis = new BufferedInputStream(response.getEntity().getContent()); } catch (IllegalStateException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } BufferedOutputStream bos = null; FileOutputStream fos = null; try { fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } bos = new BufferedOutputStream(fos); int inByte; try { while((inByte = bis.read()) != -1) bos.write(inByte); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { bis.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { bos.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(notify!=null){ notify.DownloadComplete(fileName); } return; } }