I ran into some strange problem with my Android code. I have an image in the Bitmap variable and you want to save this file to the SD card. I am using the following code,
Bitmap IMAGE // Loaded from internet servers.;
try {
File _sdCard = Environment.getExternalStorageDirectory();
File _picDir = new File(_sdCard, "MyDirectory");
_picDir.mkdirs();
File _picFile = new File(_picDir, "MyImage.jpg");
FileOutputStream _fos = new FileOutputStream(_picFile);
IMAGE.compress(Bitmap.CompressFormat.JPEG, 100, _fos);
_fos.flush();
_fos.close();
Toast.makeText(this, "Image Downloaded", 7000).show();
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(this, ex.getMessage(), 7000).show();
}
I use Sony Experia Arc as my test device, when the phone is connected to my computer, the code works well, it stores the image, and also displays it in the gallery. But when I disconnect the phone from my computer and test the application, it does not save the image and does not show any exceptions.
source
share