android - how to copy the image to folder in sdcard
<p>In my application i am downloading images from the web and these images will be stored in the sdcard. Here i am using the url as file name. For this first i am checking the file name is exits in the sdcard. if it exists then get the image from the sdcard. otherwise i am getting from web.But i am geting the following exception how to handle it. </p>

<p><strong>Exception</strong></p>

<pre><code>09-09 15:24:58.873: WARN/System.err(1117): java.io.IOException: Parent directory of file is not writable: /sdcard/http+++ecx.images-amazon.com+images+I+41KdssHpg1L._SL160_.jpg    
09-09 15:24:58.904: WARN/System.err(1117):     at java.io.File.createNewFile(File.java:1263)
09-09 15:24:58.924: WARN/System.err(1117):     at com.ibkr.elgifto.GiftCategories$itemlistadapter$4$1.run(GiftCategories.java:947)
</code></pre>

<p><strong>Code</strong></p>

<pre><code>private Drawable getDrawableFromUrl(String imageUrl) {
    String filename = imageUrl;
    filename = filename.replace("/", "+");
    filename = filename.replace(":", "+");
    filename = filename.replace("~", "s");
    final File file = new File(Environment.getExternalStorageDirectory() + File.separator + filename);
    boolean exists = file.exists();
    if (!exists) {
        try {
            URL myFileUrl = new URL(imageUrl);
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            final Bitmap result = BitmapFactory.decodeStream(is);
            is.close();
            new Thread() {
                public void run() {
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    if (result != null) {
                        result.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
                    }
                    try {
                        if (file.createNewFile()) {
                            //
                        } else {

                            //
                        }

                        FileOutputStream fo;
                        fo = new FileOutputStream(file);
                        fo.write(bytes.toByteArray());

                        fo.flush();
                        fo.close();
                        // result.recycle();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            BitmapDrawable returnResult = new BitmapDrawable(result);
            return returnResult;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    } else {
        return new BitmapDrawable(BitmapFactory.decodeFile(file.toString()));
    }
}   
</code></pre>
