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

发布于 2024-10-08 11:43:04 字数 1827 浏览 4 评论 0原文

知道为什么这不适用于运行 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):高度 = 340,宽度 = 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 图像(从网上下载)有关。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文