Reputation: 43
I am working on the it should download the file from server and store in the sdcard .
But I getting the exception : java.io.filenotfoundexception (permission denied)
public class MainActivity extends ActionBarActivity {
// usually, subclasses of AsyncTask are declared inside the activity class.
// that way, you can easily modify the UI thread from here
private class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
private PowerManager.WakeLock mWakeLock;
File file,sdcard;
public DownloadTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... sUrl) {
System.out.print("BAckground");
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream(); '
GEtting exception in line '
output = new FileOutputStream("sdcard/file.mp3");
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
Upvotes: 3
Views: 114
Reputation: 889
Do like this,
File root = new File(Environment.getExternalStorageDirectory(), "file.mp3");
if (!root.exists()) {
root.mkdirs();
}
output = new FileOutputStream(root);
And add this permission in Android menfestfile.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Upvotes: 1
Reputation: 6166
Try this code:
File yourFile = new File(Environment.getExternalStorageDirectory(),"yourfile.txt");
// replace yourfile.txt with "file.mp3" your file name
if(!yourFile.exists()) {
yourFile.createNewFile();
}
FileOutputStream output = new FileOutputStream(yourFile, false);
Also do not forget to add this permission in manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You also need to check weather your sdcard is mounted or not,
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
// Mounted
}
else {
}
Upvotes: 1
Reputation:
Try this
File root = new File(Environment.getExternalStorageDirectory(), "file.mp3");
if (!root.exists()) {
root.mkdirs();
}
output = new FileOutputStream(root);
and add permission on manifest file as
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Upvotes: 1