hanswf 2016-09-28 06:40 采纳率: 0%
浏览 3565

android保存当前view变成bitmap,并保存bitmap到本地

做了一个保存当前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

  • 写回答

1条回答

  • 徐福记456 2016-09-30 17:23
    关注

    问题在:FileNotFoundException: /sdcard/image/2016-09-28_14-31-13.png: open failed: ENOENT (No such file or directory)
    在保存到SD卡的方法中,if判断文件路径是否存在这里应该有问题,应该是if(!outFile.exists())
    我以前写过类似方法:
    private void SavePicture(Bitmap bitmap) {
    try {
    String sdcard = Environment.getExternalStorageDirectory().toString();

            File file = new File(sdcard + "/oa/myPic");
            if (!file.exists()) {
                file.mkdirs();
            }
    
            File imageFile = new File(file.getAbsolutePath(), new Date().getTime() + ".jpg");
            FileOutputStream outStream = null;
            outStream = new FileOutputStream(imageFile);
    
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();
            Toast.makeText(MessageActivity.this, "该图片保存到/oa/myPic", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!