Android将位图写入SD卡
我试图在 Android 上将位图写入 SD 卡,但收到错误消息/mnt/sdcard/PhysicsSketchpad/sketchpad145.png(没有这样的文件或目录)。
我在清单中声明了 android.permission.WRITE_EXTERNAL_STORAGE
权限,这是我的代码:
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/PhysicsSketchpad/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "sketchpad" + pad.t_id + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
发生了什么事?
更新
似乎当我尝试写入现有目录时,我收到权限被拒绝错误,
08-11 09:55:23.796: WARN/Physics Sketchpad(8881): Error when saving: IOException /mnt/sdcard/download/sketchpad54.png (Permission denied)
当我尝试保存在新目录中时,我出现没有此类文件或目录的错误,08-11 09:59:20.175: WARN/Physics Sketchpad(9040): 保存时出错:IOException /mnt/sdcard/PhysicsSketchpad/sketchpad55.png (没有这样的文件或目录)
另外,File.mkdirs()
根据是否成功返回一个布尔值,并且它返回错误的。
I'm trying to write a bitmap to an sdcard on android, and I get the error message of/mnt/sdcard/PhysicsSketchpad/sketchpad145.png (No such file or directory).
I declared the android.permission.WRITE_EXTERNAL_STORAGE
permission in the manifest, and this is my code:
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/PhysicsSketchpad/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "sketchpad" + pad.t_id + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
What's going on?
UPDATE
It seems as though when I try to write to an existing directory, I get an permission denied error,
08-11 09:55:23.796: WARN/Physics Sketchpad(8881): Error when saving: IOException /mnt/sdcard/download/sketchpad54.png (Permission denied)
and when I try to save in a new directory I get a no such file or directory error,08-11 09:59:20.175: WARN/Physics Sketchpad(9040): Error when saving: IOException /mnt/sdcard/PhysicsSketchpad/sketchpad55.png (No such file or directory)
In addition, File.mkdirs()
returns a boolean based on if it succeeded or not, and it returned false.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个代码。
try this code.
试试这个代码。这对我有用。
沙什
try this code. This has worked for me.
Shash
您获取绝对路径检查错误,您获取路径
并将位置设置
为“PhysicsSketchpad”目录未进入上述路径
尝试此操作
you getting the absolute path check into the error what you getting the path
and you set the
where as "PhysicsSketchpad" dir not getting in above path
try this