i am trying to download and store image using Async task in android , when clicking download button getting following error.
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/downloadedfilem.jpg (Permission denied)
12-07 11:01:00.120 28478-28540/myoracle.com.quotes W/System.err: at java.io.FileOutputStream.open(Native Method)
12-07 11:01:00.120 28478-28540/myoracle.com.quotes W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
12-07 11:01:00.120 28478-28540/myoracle.com.quotes W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:108)
12-07 11:01:00.120 28478-28540/myoracle.com.quotes W/System.err: at myoracle.com.quotes.WallpaperDeatilsActivity$ImageDownload.doInBackground(WallpaperDeatilsActivity.java:113)
12-07 11:01:00.120 28478-28540/myoracle.com.quotes W/System.err: at myoracle.com.quotes.WallpaperDeatilsActivity$ImageDownload.doInBackground(WallpaperDeatilsActivity.java:91)
WallpaperActivity.java
class ImageDownload extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
int count;
try {
String root = Environment.getExternalStorageDirectory().toString();
System.out.println("Downloading");
URL url = new URL(params[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
System.out.println(root);
OutputStream output = new FileOutputStream(root+"/downloadedfilem.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// writing data to file
output.write(data, 0, count);
System.out.println(count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
- Android Target Api 7.1.1
Permissions added in manifest
android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE