Setting up a download onClick. I don't get any errors...the file just never gets saved. Can anyone tell me what I missed. Pulling my hair out for 2 hours, and it's probably something so small...
Here's the code:
package com.mynavy;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.net.*;
import android.util.*;
import android.os.*;
import java.io.*;
public class YNrank extends Activity {
Button E4;
String url1 = "http://doni.daps.dla.mil/SECNAV%20Manuals1/5000.2.pdf";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rank_select);
addListenerOnButton();
}
public void addListenerOnButton()
{
E4 = (Button) findViewById(R.id.E4);
E4.setOnClickListener(new OnClickListener()
{
public void onClick(View v){
new download().execute(url1);
}
class download extends AsyncTask<String, Integer, String>{
protected String doInBackground(String... url1) {
try {
URL url = new URL(url1[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/download/E4.pdf";
InputStream in = urlConnection.getInputStream();
OutputStream output = new FileOutputStream(path);
int read = 0;
byte[] buffer = new byte[32768];
while( (read = in.read(buffer)) > 0) {
output.write(buffer, 0, read);
}
output.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
});
};}
Done some updates to the code. To answer some questions. Yes I have INTERNET and WRITE_EXTERNAL_STORAGE permissions. Took out the Toast.
LOGCAT FOR Stack Output:
11-03 05:08:13.557: D/dalvikvm(1364): GC_FOR_ALLOC freed 154K, 7% free 3682K/3952K, paused 34ms, total 46ms
11-03 05:08:14.196: W/System.err(1364): java.io.FileNotFoundException: /storage/sdcard/download/E4.pdf: open failed: ENOENT (No such file or directory)
11-03 05:08:14.206: W/System.err(1364): at libcore.io.IoBridge.open(IoBridge.java:409)
11-03 05:08:14.216: W/System.err(1364): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
11-03 05:08:14.226: W/System.err(1364): at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
11-03 05:08:14.226: W/System.err(1364): at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
11-03 05:08:14.236: W/System.err(1364): at com.mynavy.YNrank$1$download.doInBackground(YNrank.java:54)
11-03 05:08:14.236: W/System.err(1364): at com.mynavy.YNrank$1$download.doInBackground(YNrank.java:1)
11-03 05:08:14.256: W/System.err(1364): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-03 05:08:14.256: W/System.err(1364): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-03 05:08:14.266: W/System.err(1364): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-03 05:08:14.266: W/System.err(1364): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-03 05:08:14.278: W/System.err(1364): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-03 05:08:14.278: W/System.err(1364): at java.lang.Thread.run(Thread.java:841)
11-03 05:08:14.286: W/System.err(1364): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
11-03 05:08:14.296: W/System.err(1364): at libcore.io.Posix.open(Native Method)
11-03 05:08:14.306: W/System.err(1364): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-03 05:08:14.316: W/System.err(1364): at libcore.io.IoBridge.open(IoBridge.java:393)
11-03 05:08:14.316: W/System.err(1364): ... 11 more