为什么我从代码下面得到这个IOException?

时间:2013-01-29 13:26:02

标签: android file exception bitmap

我有bitmapI want to convert my bitmap to save as image in another new file by below code.

注意:除了Galaxy S3之外,它在所有设备上都能正常工作。任何人都可以帮助我使这个代码在S3中可行。转换到新文件时,我总是得到这个Toast。任何人都知道可能会发生什么问题。

Bitmap photo = (Bitmap) extras.get("data");

                selectedImagePath = String.valueOf(System.currentTimeMillis())
                        + ".jpg";

                Log.i("TAG", "new selectedImagePath before file "
                        + selectedImagePath);

                File file = new File(Environment.getExternalStorageDirectory(),
                        selectedImagePath);

                try {
                    file.createNewFile();
                    FileOutputStream fos = new FileOutputStream(file);
                    photo.compress(Bitmap.CompressFormat.PNG, 95, fos);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(this,
                            "Sorry, Camera Crashed-Please Report as Crash A.",
                            Toast.LENGTH_LONG).show();
                }

先谢谢。

1 个答案:

答案 0 :(得分:2)

存储可能未处于要写入的状态,这可能会发生多种原因。您应该在访问外部存储之前检查getExternalStorageState。即使它没有在其他设备上发生,可以发生,所以最好防范它。

如果外部存储空间为MEDIA_MOUNTED,并且您的问题仍然存在,则可以通过向e.getMessage()添加Toast来查看导致异常的原因,从而更好地处理所发生的问题或者记录。

您似乎也在使用顶级目录来存储文件。这不应该导致技术问题,但最好为您的应用创建一个子目录并在那里存储文件。来自getExternalStorageDirectory文档 -

  

应用程序不应直接使用此顶级目录   为了避免污染用户的根命名空间。

相关问题