java Android 将位图写入 sdcard
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7021728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android writing bitmap to sdcard
提问by zim333311
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:
我正在尝试将位图写入 android 上的 sdcard,我收到错误消息/mnt/sdcard/PhysicsSketchpad/sketchpad145.png (No such file or directory).
我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();
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)
当我尝试保存在新目录中时,我收到了没有此类文件或目录的错误,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.
此外,File.mkdirs()
根据是否成功返回一个布尔值,并返回false。
回答by ilango j
try this code.
试试这个代码。
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/PhysicsSketchpad";
File dir = new File(file_path);
if(!dir.exists)
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();
回答by Shash316
try this code. This has worked for me.
试试这个代码。这对我有用。
public void saveBitmap(Bitmap bm)
{
try
{
String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/";
String mFilePath = mBaseFolderPath + "abcd.jpg";
FileOutputStream stream = new FileOutputStream(mFilePath);
bm.compress(CompressFormat.JPEG, 100, stream);
stream.flush();
stream.close();
}
catch(Exception e)
{
Log.e("Could not save", e.toString());
}
}
Shash
沙什
回答by Pratik
you getting the absolute path check into the error what you getting the path
你得到了绝对路径检查错误你得到的路径
/mnt/sdcard/AppName/appname145.png
and you set the
然后你设置
Environment.getExternalStorageDirectory().getAbsolutePath() +
"/PhysicsSketchpad/";
where as "PhysicsSketchpad" dir not getting in above path
其中“PhysicsSketchpad”目录没有进入上述路径
try this
试试这个
Environment.getExternalStorageDirectory().toString()+ "/PhysicsSketchpad/";