问题
Any idea why this is not working on a HTC Hero running on Android API 1.5 ?
private static void Save_to_SD (Bitmap bm, String image_name){
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
String meteoDirectory_path = extStorageDirectory + "/Weather_Belgium";
OutputStream outStream = null;
File file = new File(meteoDirectory_path, "/"+ image_name);
try {
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Log.i("Hub", "OK, Image Saved to SD");
Log.i("Hub", "height = "+ bm.getHeight() + ", width = " + bm.getWidth());
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i("Hub", "FileNotFoundException: "+ e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.i("Hub", "IOException: "+ e.toString());
}
}
Logcat doesn't show any error and my images are normally 340x380:
12-19 12:06:41.035: INFO/Hub(1762): directory exists
12-19 12:06:41.035: INFO/Hub(1762): isDirectory = true
12-19 12:06:41.045: INFO/Hub(1762): OK, Image Saved to SD
12-19 12:06:41.045: INFO/Hub(1762): height = 340, width = 380
12-19 12:06:41.055: INFO/Hub(1762): OK, Image Saved to SD
When I then browse my SD card and take a look at my saved images, these are saved as "Invalid Image" 48 octets images !?
What's happening ?
Manifest:
<uses-sdk android:minSdkVersion="3"
android:targetSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
EDIT : it looks like this code is working properly for PNG, JPG images but the problem has to do with the fact that my images are GIF images initially (downloaded from the web).
来源:https://stackoverflow.com/questions/4482658/save-bitmap-image-to-sd-card-problem-in-api-1-5