开发者

android - how to copy the image to folder in sdcard

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 sd开发者_C百科card. otherwise i am getting from web.But i am geting the following exception how to handle it.

Exception

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

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()));
    }
}   


just add permission to AndroidManifest.xml file and check, it may work.

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


You should always check whether SD storage is available before accessing it. If it's not, you can inform the user about failure instead of crashing application. Here's the function I'm using:

public static boolean storageAvailable()
{
    String state = Environment.getExternalStorageState();
    if (!Environment.MEDIA_MOUNTED.equals(state)) return false;
    return true;
}

There's also possibility you forgot to add required permission into AndroidManifest.xml:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜