如何在Android中的内部缓存目录中创建图像文件(How to create Image File inside Internal Cache Directory in Android)

编程入门 行业动态 更新时间:2024-10-27 22:34:25
如何在Android中的内部缓存目录中创建图像文件(How to create Image File inside Internal Cache Directory in Android)

我想在Android的内部存储中的cache / images /里面有image.png。

我无法使用以下代码:

File directory = new File(getContext().getCacheDir(), "images"); directory.mkdirs(); File mypath=new File(directory,"image.png"); FileOutputStream fos = null; try { fos = new FileOutputStream(mypath); bmap.compress(Bitmap.CompressFormat.PNG, 100, fos); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } }

使用上面的代码,我甚至无法创建名为images的目录。 请帮忙,我是初学者。

I want to have image.png inside cache/images/ in Internal Storage of Android.

I am not able to have it with following code:

File directory = new File(getContext().getCacheDir(), "images"); directory.mkdirs(); File mypath=new File(directory,"image.png"); FileOutputStream fos = null; try { fos = new FileOutputStream(mypath); bmap.compress(Bitmap.CompressFormat.PNG, 100, fos); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } }

With the above code I am not even able to create a directory named images. Pls help, I am beginner.

最满意答案

尝试这个:

File sd = getCacheDir(); File folder = new File(sd, "/myfolder/"); if (!folder.exists()) { if (!folder.mkdir()) { Log.e("ERROR", "Cannot create a directory!"); } else { folder.mkdirs(); } } File fileName = new File(folder,"mypic.jpg"); try { FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName)); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }

Try this:

File sd = getCacheDir(); File folder = new File(sd, "/myfolder/"); if (!folder.exists()) { if (!folder.mkdir()) { Log.e("ERROR", "Cannot create a directory!"); } else { folder.mkdirs(); } } File fileName = new File(folder,"mypic.jpg"); try { FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName)); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }

更多推荐

本文发布于:2023-04-27 21:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   图像文件   如何在   目录中   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!