将位图图像保存到SD卡 - API 1.5中的问题?

时间:2010-12-19 11:12:02

标签: android bitmap save sd-card

知道为什么这不适用于在Android API 1.5上运行的HTC Hero吗?

    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没有显示任何错误,我的图片通常是340x380:

  

12-19 12:06:41.035:INFO / Hub(1762):目录存在

     

12-19 12:06:41.035:INFO / Hub(1762):isDirectory = true

     

12-19 12:06:41.045:INFO / Hub(1762):好的,图像已保存到SD

     

12-19 12:06:41.045:INFO / Hub(1762):height = 340,width = 380

     

12-19 12:06:41.055:INFO / Hub(1762):好的,图像已保存到SD

当我然后浏览我的SD卡并查看我保存的图像时,这些图像被保存为“无效图像”48个八位字节的图像!?

发生了什么事?

清单:

<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" />

编辑: 看起来这段代码适用于PNG,JPG图像,但问题与我的图像最初是GIF图像(从网上下载)有关。

0 个答案:

没有答案