淮阳杀人案,澳多所高校出现辱华公告,乐平教育网
第一步:获取存储的路径 我们用/sdcard/android/data/
包名/的路径 方便我们测试查看
string path=myapplication.getcontextobject().getexternalfilesdir("").tostring(); file file=new file(path);
第二步:根据该文件中存储的路径信息在文件系统上创建一个新的空文件
file finalimagefile = new file(file, system.currenttimemillis() + ".jpg"); try { finalimagefile.createnewfile(); } catch (ioexception e) { e.printstacktrace(); }
第三步:将字节放入文件输出流
fileoutputstream fos = null; try { fos = new fileoutputstream(finalimagefile); } catch (filenotfoundexception e) { e.printstacktrace(); }
第四步:将图片压缩成图片格式
bitmapdrawable bitmapdrawable = (bitmapdrawable)myapplication.getcontextobject().getresources().getdrawable(r.drawable.account); bitmap bitmap=bitmapdrawable.getbitmap(); if (bitmap == null) { toast.maketext(myapplication.getcontextobject(), "图片不存在",toast.length_long).show(); return; } bitmap.compress(bitmap.compressformat.png, 100, fos); try { fos.flush(); fos.close(); toast.maketext(myapplication.getcontextobject(), "图片保存在:"+ finalimagefile.getabsolutepath(), toast.length_long).show(); } catch (ioexception e) { e.printstacktrace(); }
完整代码
string path=myapplication.getcontextobject().getexternalfilesdir("").tostring(); file file=new file(path); file finalimagefile = new file(file, system.currenttimemillis() + ".jpg"); try { finalimagefile.createnewfile(); } catch (ioexception e) { e.printstacktrace(); } fileoutputstream fos = null; try { fos = new fileoutputstream(finalimagefile); } catch (filenotfoundexception e) { e.printstacktrace(); } bitmapdrawable bitmapdrawable = (bitmapdrawable)myapplication.getcontextobject().getresources().getdrawable(r.drawable.account); bitmap bitmap=bitmapdrawable.getbitmap(); if (bitmap == null) { toast.maketext(myapplication.getcontextobject(), "图片不存在",toast.length_long).show(); return; } bitmap.compress(bitmap.compressformat.png, 100, fos); try { fos.flush(); fos.close(); toast.maketext(myapplication.getcontextobject(), "图片保存在:"+ finalimagefile.getabsolutepath(), toast.length_long).show(); } catch (ioexception e) { e.printstacktrace(); }
总结
到此这篇关于android 获取drawable目录图片 并存入指定文件的文章就介绍到这了,更多相关android 目录图片存入指定文件内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论