【发布时间】:2012-03-10 03:21:05
【问题描述】:
我无法在 SD 卡上保存图像。我可以在 sd 卡上看到文件,但文件为空(大小为 0)。我尝试将它保存在手机内存中,它工作正常。这是我的代码。
Imagewritter {
public static boolean writeAsJPG(Context context, Bitmap bitmap,
String filename) {
filename = filename + ".jpg";
File path = Environment.getExternalStorageDirectory();
File f = new File(path, filename);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d(TAG, "file not found");
return false;
}
bitmap.compress(CompressFormat.JPEG, quality, fos);
try {
fos.flush();
fos.close();
} catch (IOException e) {
Log.d(TAG, "error closing");
e.printStackTrace();
}
}
这是位图的来源代码。
DrawingView = (drawing) findViewById(R.id.drawing_view);
drawingBitmap = (Bitmap) DrawingView.getBitmap();
String idName = timeStampText;
//save Image as JPG
ImageWritter.writeAsJPG(getApplicationContext(), drawingBitmap, idName);
【问题讨论】:
-
请贴出实际设置位图变量的代码。另外,不要分配新的 FileOutputStream。由于对 context.openFileOutput 的调用,整个代码块都被浪费了。
-
谢谢。我将添加位图来自的代码
-
你是对的。我的代码是多余的。我删除了 context.openFileOutput 并且效果很好。非常感谢
标签: android image file sd-card