将多张图像保存到SD卡

时间:2014-12-05 16:57:31

标签: android

我的项目中有30张可绘制的图片。我想通过点击按钮将所有这些图像保存/复制到SD卡。我使用下面的代码将图像保存到SD卡但我不想复制粘贴此代码30次以保存所有图像。那么有没有更好的解决方案来解决这个问题。感谢

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aurora);
        String fileName = "aurora.png";

        File sd = Environment.getExternalStorageDirectory();
        File folder = new File(sd + "/Wallpaper Pack");
        folder.mkdir();

        File dest = new File(folder, fileName);
        try {
            FileOutputStream out;
            out = new FileOutputStream(dest);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

2 个答案:

答案 0 :(得分:2)

非常简单,创建一个数组并循环它。

int[] drawablesArr = {R.id.name1, R.id.name2, ....}

for(int i=0l i<=drawablesArr.length; i++){
     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawablesArr[i]);
        String fileName = "image_"+ String.valueOf(i)+".png" ;

        File sd = Environment.getExternalStorageDirectory();
        File folder = new File(sd + "/Wallpaper Pack");
        folder.mkdir();

        File dest = new File(folder, fileName);
        try {
            FileOutputStream out;
            out = new FileOutputStream(dest);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

答案 1 :(得分:0)

@Murtaza Hussain的答案是正确的,但最好是从UI(主)线程中运行此类操作。因此,您可以使用ThreadPoolExecutor

// SaveThread.java
public class SaveThread implements Runnable {

    private int drawable;
    private String fileName;
    private Context context;

    public SaveThread(Context context, int drawable, String fileName) {
        this.drawable = drawable;
        this.fileName = fileName;
        this.context = context;
    }

    @override
    public void run() {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawable);

        File dest = new File(Environment.getExternalStorageDirectory(), "WallpaperPack/" + fileName);
        dest.getParentFile().mkdirs();

        try {
            FileOutputStream out = new FileOutputStream(dest);
            bitmap.compress(CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

然后在您的活动或其他组件中:

int core = Runtime.getRuntime().availableProcessors();
ExecutorService executor =
        new ThreadPoolExecutor(
                core + 1,
                core * 2 + 1,
                60l,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>()
                );

int[] drawables = {R.id.name1, R.id.name2, ....}

for(int drawable : drawables) {
    executor.execute(new SaveThread(getApplicationContext(), drawable, "image_"+ drawable +".png"));
}

executor.shutdown();