用的是这位前辈给的方式,但是不知道为什么点了保存之后什么都没有
我的代码:
public void onDraw(Canvas canvas)
{
canvas.drawColor(0xFFCC0000);
int nCanvasWidth = canvas.getWidth();
int nCanvasHeight = canvas.getHeight();
int nBitmapWidth = mBottomBitmap.getWidth();
int nBitmapHeight = mBottomBitmap.getHeight();
mBottomBitmapDrawHeight = (nCanvasHeight - nBitmapHeight)/2;
canvas.drawBitmap(mBottomBitmap,0,mBottomBitmapDrawHeight,mBitmapPaint);
mCanvas.save(Canvas.ALL_SAVE_FLAG);
mCanvas.restore();
if(mPath != null)
{
canvas.drawPath(mPath, mPaint);
}
}
/*
中间是touchview绘图方法
*/
public void saveImage()
{
File file = new File("/sdcard/akai/");
if(!file.exists())
file.mkdirs();
try
{
FileOutputStream fos = new FileOutputStream(file.getPath() + "/2.png");
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
System.out.println("saveBmp is here");
}
catch(Exception e){
e.printStackTrace();
}
}
需要在AndroidManifest文件中加入:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
图片保存成功后,你要通知图库进行更新。
/**
* 保存图片到图库
* @param context
* @param bmp
*/
public static void saveImageToGallery(Context context, Bitmap bmp) {
// 首先保存图片
File appDir = new File(Environment.getExternalStorageDirectory(),
"desheng");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
MyToastUtils.showShortToast(context, "保存失败");
e.printStackTrace();
} catch (IOException e) {
MyToastUtils.showShortToast(context, "保存失败");
e.printStackTrace();
}
// 其次把文件插入到系统图库
try {
MediaStore.Images.Media.insertImage(context.getContentResolver(),
file.getAbsolutePath(), fileName, null);
MyToastUtils.showShortToast(context, "保存成功");
} catch (FileNotFoundException e) {
MyToastUtils.showShortToast(context, "保存失败");
e.printStackTrace();
}
// 最后通知图库更新
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(new File(file.getPath()))));
}
最可能的原因是你忘了加权限了