如何在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(); }更多推荐
发布评论