8

I want to save image into my local drive folder or to res/drawable folder in my application. I'm right now saving img into sd card but I've to save it in res/drawable folder. My code is:

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();
        }

    }

};
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
Android
  • 8,995
  • 9
  • 67
  • 108

3 Answers3

6

Its not possible.

The following link can be helpful

Community
  • 1
  • 1
chiranjib
  • 5,288
  • 8
  • 53
  • 82
3

It is not possible to save anything into res/drawable at runtime, sorry.

Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
1

You can't modify neither res/drawable, nor any other folder of your apk at runtime. It is build at compile-time and nothing can modify it from inside the application after.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103