I have small app written in android.
With AsyncTask I'm trying to download images from server and store them on external or internal storage on my phone.
The images are in format jpg, png etc.
@Override
protected String doInBackground(String... param)
{
try
{
for(int i = 0; i < param.length; i++)
{
String src = "http://myLINK/images/"+param[i];
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.connect();
InputStream input = connection.getInputStream();
images.add(BitmapFactory.decodeStream(input));
if (images.get(i) != null){
saveImageToExternalStorage(images.get(i), param[i]);
}
}
}
catch (IOException io_exception){
Log.e("ERROR", io_exception.getMessage());
}
return "success";
}
// save image to external storage
private void saveImageToExternalStorage(Bitmap image, String image_name)
{
try
{
String state = Environment.getExternalStorageState();
String path;
// check if external storage exists
if (Environment.MEDIA_MOUNTED.equals(state))
path = Environment.getExternalStorageDirectory().toString();
else
path = Environment.getDataDirectory().toString();
OutputStream fOut;
File file = new File(path, "myDIR/images/"+image_name);
fOut = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
}
catch(Exception e){
Log.e("ERROR", e.getMessage());
}
}
There error is:
/storage/emulated/0/myDIR/images/57d7fa462860b_image1.jpeg: open failed: ENOENT (No such file or directory)
Can anyone help me with this?