1.参考
//得到外部存储卡的路径
String path=Environment.getExternalStorageDirectory().toString();
//ff.png是将要存储的图片的名称
File file=new File(path, "ff.png");
//从资源文件中选择一张图片作为将要写入的源文件
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ss);
try {
FileOutputStream out=new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
一定要不要忘记加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.应用
public void drawableTopicture(Drawable drawable) {
/*
* Drawable转化为Bitmap
*/
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0,0,width,height);
drawable.draw(canvas);
String filepath = null;
/*创建目录*/
if(Tools.hasSdcard()){
filepath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/HeadImage";
File f = new File(filepath);
if(!f.exists()){
f.mkdir();
}
}
headImgae_filename = new File(filepath,"headimage.png");
/*将bitmap保存为图片文件*/
FileOutputStream fos = null;
try {
fos = new FileOutputStream(headImgae_filename);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
来源:oschina
链接:https://my.oschina.net/u/1014520/blog/201434