2011-03-29 2 views
6

내 로컬 드라이브 폴더 또는 내 응용 프로그램의 res/drawable 폴더에 이미지를 저장하고 싶습니다. 지금은 SD 카드에 img를 저장하고 있지만 res/drawable 폴더에 저장해야합니다. 내 코드는 다음과 같습니다android image res/drawable 폴더에 저장

String image_URL = "http://chart.apis.google.com/chart?chs=200x200&cht=qr&chl=http%3A%2F%2Fandroid-er.blogspot.com%2F"; 

String extStorageDirectory; 

Bitmap bm; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button buttonSave = (Button) findViewById(R.id.save); 

    ImageView bmImage = (ImageView) findViewById(R.id.image); 
    BitmapFactory.Options bmOptions; 
    bmOptions = new BitmapFactory.Options(); 
    bmOptions.inSampleSize = 1; 
    bm = LoadImage(image_URL, bmOptions); 
    bmImage.setImageBitmap(bm); 


    extStorageDirectory = Environment.getExternalStorageState().toString(); 
    extStorageDirectory = Environment.getExternalStorageDirectory() 
      .toString(); 

    buttonSave.setText("Save to " + extStorageDirectory + "/qr.PNG"); 
    buttonSave.setOnClickListener(buttonSaveOnClickListener); 
} 

private Bitmap LoadImage(String URL, BitmapFactory.Options options) { 
    Bitmap bitmap = null; 
    InputStream in = null; 
    try { 
     in = OpenHttpConnection(URL); 
     bitmap = BitmapFactory.decodeStream(in, null, options); 
     in.close(); 
    } catch (IOException e1) { 
    } 
    return bitmap; 
} 

private InputStream OpenHttpConnection(String strURL) throws IOException { 
    InputStream inputStream = null; 
    URL url = new URL(strURL); 
    URLConnection conn = url.openConnection(); 

    try { 
     HttpURLConnection httpConn = (HttpURLConnection) conn; 
     httpConn.setRequestMethod("GET"); 
     httpConn.connect(); 

     if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
      inputStream = httpConn.getInputStream(); 
     } 
    } catch (Exception ex) { 
    } 
    return inputStream; 
} 

Button.OnClickListener buttonSaveOnClickListener = new Button.OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     OutputStream outStream = null; 
     File file = new File(extStorageDirectory, "er.PNG"); 
     try { 
      outStream = new FileOutputStream(file); 
      bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
      outStream.flush(); 
      outStream.close(); 

      Toast.makeText(LoadSaveImgActivity.this, "Saved", 
        Toast.LENGTH_LONG).show(); 

     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Toast.makeText(LoadSaveImgActivity.this, e.toString(), 
        Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Toast.makeText(LoadSaveImgActivity.this, e.toString(), 
        Toast.LENGTH_LONG).show(); 
     } 

    } 

}; 

답변

3

죄송합니다, 런타임에 res/drawable에 아무것도 저장할 수 없습니다.

+3

도움이 될 수 있습니다 ..! ;) – Mudassir

1

런타임에는 ap/res/drawable 또는 apk의 다른 폴더를 수정할 수 없습니다. 컴파일시에 빌드되며 이후에는 응용 프로그램 내부에서 수정할 수 없습니다.

관련 문제