public void saveBitmap() {
Toast.makeText(MainActivity.this, "保存图片成功", 1).show();
String state=Environment.getExternalStorageState();
File file;
try {
if(Environment.MEDIA_MOUNTED.equals(state)){
// 保存图片到SD卡上
file = Environment.getExternalStorageDirectory();
}else{
file =getFilesDir();
}
FileOutputStream stream = new FileOutputStream(file.getPath()+System.currentTimeMillis()+".png");
baseBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Toast.makeText(MainActivity.this, "保存图片成功", 0).show();
// Android设备Gallery应用只会在启动的时候扫描系统文件夹
// 这里模拟一个媒体装载的广播,用于使保存的图片可以在Gallery中查看鿴
// Intent intent = new Intent();
// intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
// intent.setData(Uri.fromFile(Environment.getDataDirectory()));
// sendBroadcast(intent);
stream.flush();
stream.close();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "保存图片失败", 1).show();
e.printStackTrace();
}
}
输出“保存图片失败”, 上面这一句也不会被执行:Toast.makeText(MainActivity.this, "保存图片成功", 1).show();为什么呢?但是把try下面的注释了就能执行到,求解!怎样才能保存在手机内存中?