Mar*_*ner 4 java android android-file
我有一个位图,我想将其保存到应用程序文件夹中。我尝试使用这些代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
现在,我有两个问题。
“File.mkdir()”的结果被忽略
小智 5
你可以这样做:
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();
}
Run Code Online (Sandbox Code Playgroud)
它将您的位图保存在应用程序文件夹的“文件”目录中。
| 归档时间: |
|
| 查看次数: |
8414 次 |
| 最近记录: |