android图像保存到res / drawable文件夹[复制]

参见英文答案 > Write to /res/drawable/ on the fly?3个
我想将图像保存到我的本地驱动器文件夹或我的应用程序中的res / drawable文件夹.我现在正在将img保存到SD卡中,但我要将它保存在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,Toast.LENGTH_LONG).show();
        }

    }

};

解决方法

这是不可能的.

以下链接可能会有所帮助

> Write to /res/drawable/ on the fly?

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...