我正在开发一个应用程序,我想在其中将一些位图保存到 SD 卡中。我查看了很多示例和其他问题,并据此编写了以下代码:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String dirPath = Environment.getExternalStorageDirectory().toString() + "/myFolder";
File dir = new File(dirPath);
dir.mkdirs();
String fileName = "bitmapname.jpg";
File file = new File(dirPath, fileName);
FileOutputStream fileOutPutStream;
try {
boolean created = file.createNewFile();
Log.d("Checks", "File created: " + created);
fileOutPutStream = new FileOutputStream(file);
fileOutPutStream.write(byteArrayOutputStream.toByteArray());
fileOutPutStream.close();
} catch (FileNotFoundException e) {
Log.d("Checks", "FileNotFoundException");
e.printStackTrace();
} catch (IOException e) {
Log.d("Checks", "IOException");
Log.d("Checks", e.getMessage());
e.printStackTrace();
}
我看不出这段代码有什么问题。它不会给出任何错误,我的应用程序运行时不会崩溃。但是,当我将手机连接到电脑并打开 SD 卡时,我没有看到文件夹“myFolder”,也无法在任何地方找到保存的图像。你们知道这是为什么吗?
编辑: 我注意到我可以在 Android 画廊中看到保存的位图,它们确实位于名为“myFolder”的文件夹中。但是,当我将手机连接到计算机并浏览我的 SD 卡时,我仍然看不到它们。
最佳答案
根据我的经验,当我忘记 close()
之前的 fileOutPutStream.flush();
时,我遇到了类似的问题。
关于android - 保存的位图没有出现在 SD 卡上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16628697/