保存 BitmapDrawable 或位图
好的,所以我有一个代码,它拍摄当前视图的图像,并将其转换为位图,然后我最终来到这里,
Bitmap bm = view.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
现在,我想做的是拍摄当前视图的照片就像,但是从这里,我可以轻松地将 bitmapDrawable
放入 ImageView 中,但这不是我想要的,我想从这里开始保存它。我能做些什么?我找到了一种方法,使用
FileOutputStream fos;
try {
fos = super.openFileOutput("output.png",MODE_WORLD_READABLE);
bm.compress(CompressFormat.PNG, 75, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
但是当我这样做时,我最终会遇到 NullPointerException ,
bm.compress(CompressFormat.PNG, 75, fos);
我是否遗漏了一些东西?
好的,现在它通过了上面的代码,然后通过 fos.close(); 完成,但是什么也没有发生,它没有保存,没有保存在我的手机上,什么都没有
Okay, so I have a code, that take an image of the current view, and turns it into a bitmap, then I end up here,
Bitmap bm = view.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
now, what I'm trying to do, is take a picture of what the current view looks like, but from here, I can easily put bitmapDrawable
into an ImageView, but thats not what I want, I'd like to go from here, to saving it. What can I do? I found out one way, using
FileOutputStream fos;
try {
fos = super.openFileOutput("output.png",MODE_WORLD_READABLE);
bm.compress(CompressFormat.PNG, 75, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
but when I do this, I end up with a NullPointerException at
bm.compress(CompressFormat.PNG, 75, fos);
am I missing something?
Okay, now it makes it through the above code, then makes it past fos.close();
to finish, but then nothing happens, its not saved, not on my phone, nothing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该行上唯一可以为 null(并导致 NullPointerException)的是
bm
,这意味着Bitmap bm = view.getDrawingCache();
没有按预期工作。你能发布堆栈跟踪吗?The only thing that can be null on that row (and cause a NullPointerException) is
bm
, which meansBitmap bm = view.getDrawingCache();
didn't work as intended. Can you post the stack trace?