我有一个位图,我想将其保存到应用程序文件夹中。我尝试使用这些代码:
ContextWrapper contextWrapper = new ContextWrapper(context.getApplicationContext());
File directory = contextWrapper.getDir("tabs", Context.MODE_PRIVATE);
if (!directory.exists())
directory.mkdir();
String fname = "Image.jpg";
File file = new File(directory, fname);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
fos.close();
} catch (Exception e) {
Log.e("SAVE_IMAGE", e.getMessage(), e);
}
现在,我有两个问题。
- 为什么会显示此警告以及如何修复它?
Result of 'File.mkdir()' is ignored
- 在应用程序文件夹中创建了目录“app_tabs”,但未保存位图,并且该文件夹中没有照片。如何保存该位图?
最佳答案
你可以这样做:
try {
FileOutputStream fileOutputStream = context.openFileOutput("Your File Name", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
它将您的位图保存在应用程序文件夹的“files”目录中。
关于java - 如何将位图保存到android中的应用程序文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61382955/