无法将图像从资产文件夹复制到SD卡

时间:2014-01-21 07:45:57

标签: android android-sdcard android-assets

我正在尝试将有效图片(jpg)从资产文件夹复制到SD卡。 我对清单(和读取)有写权限。 这是我使用的功能,并为其提供资产文件夹中列出的“image.jpg”。 当我进入外部存储目录的路径时,我可以看到副本已经制作,并且有一个“image.jpg”文件,但是,它不是有效的图像,因为我无法打开它。

private void copyFile(String fileName)
{
    try
    {
        File f = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
        if (f.exists())
        {
            return;
        }
        InputStream in = getResources().getAssets().open(fileName);
        OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + fileName);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1)
        {
            out.write(buffer, 0, read);
        }
        in.close();
        out.flush();
        out.close();
    }
    catch (IOException e)
    {
        Toast.makeText(/* getActivity() */this, "Failed to copy file: " + fileName, Toast.LENGTH_SHORT).show();
    }
}

任何帮助将不胜感激:)

2 个答案:

答案 0 :(得分:0)

以下是我的示例代码(将Assets文件夹复制到Storage文件夹)

public boolean copyAssetToStorage(final Activity activity, final String from, final String to) {

        final AssetManager assetManager = activity.getAssets();

        final File outDir;
        if ( TextUtils.isEmpty(to) ) {
            outDir = rootFile;
        } else {
            outDir = new File(rootFile, to);
            outDir.mkdir();
        }

//      if (TextUtils.isEmpty(from)) {
//          new RuntimeException("From Path is Empty String.").printStackTrace();
//          return false;
//      }
        if (from == null) {
            new RuntimeException("From Path is Null String.").printStackTrace();
            return false;
        }

        final String[] assets;
        try {
            assets = assetManager.list(from);
        } catch (IOException e1) {
            LogUtil.e("AssetManager.list() 에러");
            e1.printStackTrace();
            return false;
        }

        Thread copyThread = new Thread(new Runnable() {

            @Override
            public void run() {

                for (String fileName : assets) {
                    InputStream is;
                    FileOutputStream fos;
                    LogUtil.i("복사 시작: "+fileName); // Copy Start!
                    try {
                        is = assetManager.open(from+File.separator+fileName);
                        int size = is.available();
                        byte[] buffer = new byte[size];
                        File outFile = new File(outDir, fileName);
                        fos = new FileOutputStream(outFile);
                        for (int c = is.read(buffer); c != -1; c=is.read(buffer)) {
                            fos.write(buffer, 0, c);
                        }
                        is.close();
                        fos.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                        return;
                    }
                    LogUtil.i("복사 완료: "+fileName); // Completed!!!
                }

            }
        }, "CopyThread");

        copyThread.start();

        return true;
    }

答案 1 :(得分:0)

您可以将图片放入res中的其他方式 - >原始文件夹
此代码可以将所有文件从您的res复制到SD卡 - >原始文件夹

    java.lang.reflect.Field[] fields=R.raw.class.getFields();
    for(int count=0; count < fields.length; count++){
        try { 
            int resourceID=fields[count].getInt(fields[count]);
            InputStream in = getResources().openRawResource(resourceID);
            OutputStream out = new FileOutputStream("/sdcard/"+fields[count].getName()+".png");
            byte[] buf = new byte[2048];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }   
            in.close(); 
            out.close();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }