-1

I want to download a picture from internet url and save it in the external storage. Before i've tried to create a folder called MyApp, but seems to be failing because the logs aren't appearing at the console. This is my code:

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/MyApp/";
File fileFolder = new File(path);

try {
    // Make sure the Pictures directory exists.
    if (!fileFolder.exists()) {
        if (!fileFolder.mkdirs()) {
            Log.i("myApp", "failed to create directory");
        }else{
            Log.i("myApp", "the dir has been created");
        }
    }

    newFoto = new File(path, urls[1]);

    URL url = new URL(urls[0]);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    is = connection.getInputStream();

    OutputStream os = new FileOutputStream(newFoto);

    byte[] buffer = new byte[is.available()];
    int bytesRead = 0;
    while ((bytesRead = is.read(buffer, 0, buffer.length)) >= 0) {
        os.write(buffer, 0, bytesRead);
    }


    Log.i("myApp", "writing at"+newFoto.getPath());
    is.close();
    os.close();

    return true;
} catch (IOException e) {
    Log.i("myApp", "Download has failed: " + e);
    return false;
}   
Bhargav
  • 8,118
  • 6
  • 40
  • 63
Mario Muñoz
  • 3
  • 1
  • 1
  • hey what log line do you expect to show? or no logs show? did you change log level to Info? – linhtruong Dec 02 '15 at 10:14
  • Logs like the dir has been created or failed to create directory are not appearing. But i've checked the folders with fileManager and the dir has been created, but the picture appears like a broken file. I don't know if i'm doing the outputstream wrong. In apis lower than 19 is working good this code. – Mario Muñoz Dec 02 '15 at 10:25
  • Regarding the logs of the dir, you should delete your dir and run again to see the logs because if the dir was created, code will not run into the if case for sure! – linhtruong Dec 02 '15 at 10:30
  • hmm can you show full of your function/class so its easy to check – linhtruong Dec 02 '15 at 10:31

2 Answers2

0

First add a permission to your manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />

Then in your code

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/MyApp/";
// this provides path to the phone memory available to you, not an external sdcard.
File fileFolder = new File(path);

try {
  // Make sure the Pictures directory exists.
  if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && !fileFolder.exists()) {
    if (!fileFolder.mkdirs()) {
      Log.i("myApp", "failed to create directory");
    } else {
      Log.i("myApp", "the dir has been created and path : "+ fileFolder.getPath());
    }
  } else {
    Log.i("myApp", "unable to write to external dir");
  }
  newFoto = new File(path, "filename.extension");
  // i don't know what urls[1] is returning so for the moment filename.extension

  URL url = new URL(urls[0]);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  is = connection.getInputStream();

  OutputStream os = new FileOutputStream(newFoto);

  byte[] buffer = new byte[is.available()];
  int bytesRead = 0;
  while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) {
    os.write(buffer, 0, bytesRead);
  }

  Log.i("myApp", "writing at" + newFoto.getPath());
  is.close();
  os.close();

  return true;
} catch (IOException e) {
  Log.i("myApp", "Download has failed: " + e);
  return false;
}

this should solve your issue.

Akash Raghav
  • 1,073
  • 9
  • 19
  • Thank you, now the directory has been created but something is failing when downloading the picture because appears like broken file within the folder, could you suggest me how to download files from url for this api level? i think that my problem is with the outputstream, is not writting the bytes properly but i don't know. – Mario Muñoz Dec 02 '15 at 10:54
  • read this - http://stackoverflow.com/questions/18210700/best-method-to-download-image-from-url-in-android – Akash Raghav Dec 02 '15 at 12:00
0

Check if you have declared these permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

Other things you could/should change in your catch block is:

Log.i("myApp", "Download has failed: " + e);

to

Log.e("myApp", "Download has failed: " + e.getMessage(), e);

In case you still can't see any log, Switch the filter from "Show only selected application" to "No filters".

After that, please post the log otherwise it's quite hard to understand what's going wrong.

Make sure to check the Android Documentation on how to store files in disk.

I would also suggest to use a library for image loading like Picasso or UniversalImageLoader

fedestylah
  • 169
  • 1
  • 10