机器人 - 如何复制SD卡中的文件夹到图像 [英] android - how to copy the image to folder in sdcard

查看:151
本文介绍了机器人 - 如何复制SD卡中的文件夹到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我从网络上下载的图像和这些图像将被存储在SD卡中。在这里,我使用的URL作为文件名。对于这第一个我检查的文件名是在SD卡退出。如果存在,那么您可以通过SD卡图像。否则我是从web.But得到我正在歌厅以下异常如何处理它。

例外

  15 09-09:24:58.873:WARN / System.err的(1117):java.io.IOException异常:文件的上级目录不可写:/ SD卡/ HTTP ++ + ecx.images-amazon.com +图片+ I + 41KdssHpg1L._SL160_.jpg
09-09 15:24:58.904:WARN / System.err的(1117):在java.io.File.createNewFile(File.java:1263)
09-09 15:24:58.924:WARN / System.err的(1117):在com.ibkr.elgifto.GiftCategories $ itemlistadapter $ 4 $ 1.run(GiftCategories.java:947)

code

 私人绘​​制对象getDrawableFromUrl(字符串图片网址){
    字符串文件名=图片网址;
    文件名= filename.replace(/,+);
    文件名= filename.replace(:,+);
    文件名= filename.replace(〜,S);
    最终文件的文件=新的文件(Environment.getExternalStorageDirectory()+文件分割符+文件名);
    布尔存在= file.exists();
    如果(!存在){
        尝试{
            网址myFileUrl =新的URL(图片网址);
            HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl.openConnection();
            conn.setDoInput(真);
            conn.connect();
            InputStream为= conn.getInputStream();
            最终的结果是位图= BitmapFactory.de codeStream(是);
            is.close();
            新的Thread(){
                公共无效的run(){
                    ByteArrayOutputStream字节=新ByteArrayOutputStream();
                    如果(结果!= NULL){
                        result.com preSS(Bitmap.Com pressFormat.JPEG,40字节);
                    }
                    尝试{
                        如果(file.createNewFile()){
                            //
                        }其他{                            //
                        }                        FileOutputStream中FO;
                        FO =新的FileOutputStream(文件);
                        fo.write(bytes.toByteArray());                        fo.flush();
                        fo.close();
                        // result.recycle();
                    }赶上(IOException异常五){
                        e.printStackTrace();
                    }
                }
            }。开始();
            BitmapDrawable returnResult =新BitmapDrawable(结果);
            返回returnResult;
        }赶上(FileNotFoundException异常五){
            e.printStackTrace();
        }赶上(MalformedURLException的E){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
        返回null;
    }其他{
        返回新BitmapDrawable(BitmapFactory.de codeFILE(file.toString()));
    }
}


解决方案

您应经常检查SD存储是否在访问之前可用。如果不是,你可以告知失败,而不是崩溃应用程序的用户。下面是我使用的功能:

 公共静态布尔storageAvailable()
{
    字符串状态= Environment.getExternalStorageState();
    如果(!Environment.MEDIA_MOUNTED.equals(州))返回false;
    返回true;
}

还有你忘了添加所需的权限进入的Andr​​oidManifest.xml可能性:

 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>

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.

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

解决方案

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" />

这篇关于机器人 - 如何复制SD卡中的文件夹到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆