我在将图像保存到 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);
最佳答案
我认为你必须在 list 文件中添加这些权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
关于android - 将文件保存在 SD 卡上,写入文件大小 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9643661/