做了一个保存当前view成bitmap图片的项目,但最终报如下错误,找了半天也找不到解决之法。
public static Bitmap getBitmapByView(ScrollView scrollView, HorizontalScrollView hsv) {
int w = 0;
int h = 0;
Bitmap bitmap = null;
// 获取scrollview实际高度
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
}
// 获取HorizontalScrollView实际宽度
for (int i = 0; i < hsv.getChildCount(); i++) {
w += hsv.getChildAt(i).getWidth();
hsv.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
}
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
scrollView.draw(canvas);
hsv.draw(canvas);
return bitmap;
}
/**
* 压缩图片
*
* @param image
* @return
*/
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
// 把压缩后的数据baos存放到ByteArrayInputStream中
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
// 把ByteArrayInputStream数据生成图片
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
/**
* 保存到sdcard
*
* @param b
* @return
*/
public static String savePic(Bitmap b) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss",
Locale.US);
File outfile = new File("/sdcard/image");
// 如果文件不存在,则创建一个新文件
if (!outfile.isDirectory()) {
try {
outfile.mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
String fname = outfile + "/" + sdf.format(new Date()) + ".png";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fname);
if (null != fos) {
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fname;
}
---------------------------------------------
09-28 14:31:11.181 23514-23514/com.bonc.ms.main I/ViewRootImpl: finishMotionEvent: handled = true stage=10: View Post IME stage,inputElapseTime=0 eventTime = 867860380 downTime = 867860380 title= com.bonc.ms.main/com.bonc.ms.main.develop.FourGDevelopActivity
09-28 14:31:11.287 23514-23514/com.bonc.ms.main V/SettingsInterface: invalidate [system]: current 4765 != cached 0
09-28 14:31:11.289 23514-23514/com.bonc.ms.main D/ActivityThread: holder:android.app.IActivityManager$ContentProviderHolder@30ba67e, holder.provider:android.content.ContentProviderProxy@c2477df
09-28 14:31:11.398 23514-23520/com.bonc.ms.main W/art: Suspending all threads took: 58.163ms
09-28 14:31:12.773 23514-23724/com.bonc.ms.main D/skia: jpeg_decoder mode 1, colorType 4, w 7907, h 4586, sample 1, bsLength 0!!
09-28 14:31:13.227 23514-23724/com.bonc.ms.main D/skia: jpeg_decoder finish successfully, L:1934!!!
09-28 14:31:13.229 23514-23724/com.bonc.ms.main W/System.err: java.io.FileNotFoundException: /sdcard/image/2016-09-28_14-31-13.png: open failed: ENOENT (No such file or directory)
09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at libcore.io.IoBridge.open(IoBridge.java:487)
09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at java.io.FileOutputStream.(FileOutputStream.java:87)
09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at java.io.FileOutputStream.(FileOutputStream.java:127)
09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at java.io.FileOutputStream.(FileOutputStream.java:116)
09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at com.bonc.ms.tools.ScreenShot.savePic(ScreenShot.java:110)
09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at com.bonc.ms.main.develop.FourGDevelopActivity$3.run(FourGDevelopActivity.java:153)
09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at java.lang.Thread.run(Thread.java:818)
09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
09-28 14:31:13.232 23514-23724/com.bonc.ms.main W/System.err: at libcore.io.Posix.open(Native Method)
09-28 14:31:13.232 23514-23724/com.bonc.ms.main W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
09-28 14:31:13.232 23514-23724/com.bonc.ms.main W/System.err: at libcore.io.IoBridge.open(IoBridge.java:473)
09-28 14:31:13.232 23514-23724/com.bonc.ms.main W/System.err: ... 6 more