Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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();
        }

    }

};

解决方法

大佬总结

以上是大佬教程为你收集整理的android图像保存到res / drawable文件夹[复制]全部内容,希望文章能够帮你解决android图像保存到res / drawable文件夹[复制]所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。