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

这篇博客介绍了如何在Android应用中从网络加载QR码图像,并将其保存到本地SD卡。代码示例展示了如何使用BitmapFactory从URL获取图像,然后将其保存到外部存储。然而,Android的res/drawable目录不允许运行时动态写入,通常需要在编译时静态添加资源。
摘要由CSDN通过智能技术生成

参见英文答案 >

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

}

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值