【发布时间】:2012-10-19 03:39:55
【问题描述】:
我正在尝试将图像文件写入特定目录中的公共图库文件夹,但我不断收到错误消息,因为它是一个目录,所以无法打开该文件。
到目前为止,我有以下内容
//set the file path
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;
File outputFile = new File(path,"testing.png");
outputFile.mkdirs();
FileOutputStream out = new FileOutputStream(outputFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
其中目录是应用程序名称。所以应用程序保存的所有照片都会进入该文件夹/目录,但我一直收到错误
/storage/sdcard0/Pictures/appname/testing.png: open failed: EISDIR (Is a directory)
即使我不尝试将其放在目录中并将变量路径转换为类似文件
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
我没有收到错误,但照片仍未显示在图库中。
***回答 问题是,当我最初运行此代码时,它创建了一个名为 testing.png 的目录,因为在目录中创建文件之前我未能创建目录。所以解决方案是先制作目录,然后用一个单独的文件写入它,像这样
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + directory;
//directory is a static string variable defined in the class
//make a file with the directory
File outputDir = new File(path);
//create dir if not there
if (!outputDir.exists()) {
outputDir.mkdir();
}
//make another file with the full path AND the image this time, resized is a static string
File outputFile = new File(path+File.separator+resized);
FileOutputStream out = new FileOutputStream(outputFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
请注意,如果您犯了与我开始时相同的错误,您可能需要进入存储并手动删除目录
【问题讨论】:
-
有时需要刷新图库。通过 ddms 转到您的文件夹路径并检查您的新文件是否已创建。
-
是的,我做了很多次都没有运气
-
要扫描您的新文件,请参阅此 [post][1] [1]:stackoverflow.com/questions/4646913/…
标签: android android-sdcard android-image