2014-12-05 2 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

+0

offcourse, 그가 할 수있는 일 ... –

+0

@Murtaza Hussain이 코드에 감사드립니다. 귀하의 코드를 시도했지만 필요한 변경을 지적하기 위해 Ascorbin 덕분에 하나의 이미지 만 저장할 수있었습니다. 이제 작동 :) 감사합니다. 그리고 자동화 된 이름 대신 내 자신의 파일 이름을 사용할 수 있습니까? – user2551070

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

나는 안드로이드 개발을 처음 접했기 때문에 코드를 이해하는 것이 어렵다. 코드에 대해 감사한다. – user2551070

+0

@ user2551070 UI 쓰레드에서주의 깊게 오랫동안 실행중인 작업은 ANR 예외를 유발할 수 있으며 다른 장치에서 다릅니다! –

+0

나는 내 애플 리케이션에서 당신의 접근 방식을 이해하고 희망적으로 적용하려고 노력할 것이다. 코드 스 니펫을 작성해 주셔서 감사합니다 :) – user2551070

관련 문제