2014-12-05 126 views
0

我的項目在繪製& 30張圖片我想保存/所有這些圖像到SD卡複製點擊一個按鈕。我使用下面的代碼將圖像保存到SD卡,但我不想將此代碼複製粘貼30次以保存所有圖像。那麼是否有更好的解決方案來解決這個問題。謝謝將多幅圖片保存到SD卡

 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

它非常簡單,創建一個數組並循環它。

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

你必須改變在每個迭代上的文件名。 – FWeigl 2014-12-05 17:04:39

+0

offcourse,那他可以做... – 2014-12-05 17:05:24

+0

@Murtaza Hussain謝謝你的代碼。我嘗試了你的代碼,但只能保存一張圖片,這要感謝Ascorbin指出必要的改變。現在它工作:)謝謝你們。是否有可能使用我自己的文件名而不是自動命名? – user2551070 2014-12-05 18:00:10

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(); 
+0

由於我是新來的Android開發這真的很難,我理解你的代碼,但無論如何,感謝代碼:) – user2551070 2014-12-05 18:04:29

+0

@ user2551070 OK,但要小心,長期運行在UI線程操作可能會導致ANR例外,它在不同的設備中是不同的! – 2014-12-06 05:28:22

+0

我會盡力瞭解並希望在您的應用中應用您的方法。感謝您花時間編寫代碼片段:) – user2551070 2014-12-06 09:48:56