Android错误将图片保存到SD卡

时间:2012-06-28 13:00:33

标签: android camera image sd-card

我有一个Android应用程序,它在某些手机上崩溃,而不是在其他手机上崩溃。它的下载量约为5k,到目前为止已收到约50份崩溃报告。但是,根据我从用户那里得到的评级/评论数量来说,它会强行崩溃,我认为受影响用户的百分比实际上要高得多。

由于问题不影响我的手机,我无法重现错误并按照我的意愿进行调试。

应用程序从相机拍摄照片,在其上覆盖位图,并将生成的图像保存到SD卡。

以下是onPictureTaken PictureCallback方法的代码。

    private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

    public void onPictureTaken(byte[] data, Camera camera) {

        captureBtn.setVisibility(ImageButton.INVISIBLE); 

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
            return;
        }

        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap mutableBitmap = null;
        Bitmap finalBitmap = null; 
        byte[] byteArray = null; 
        try {   
            mutableBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options).copy(Bitmap.Config.RGB_565, true);
            Matrix matrix = new Matrix();
            int width = mutableBitmap.getWidth();
            int height = mutableBitmap.getHeight();
            int newWidth = overlayView.getDrawable().getBounds().width();
            int newHeight = overlayView.getDrawable().getBounds().height();
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            matrix.postScale(scaleWidth, scaleHeight);
            matrix.postRotate(90);

            Bitmap resizedBitmap = Bitmap.createBitmap(mutableBitmap, 0, 0, mutableBitmap.getWidth(), mutableBitmap.getHeight(), matrix, true);
            finalBitmap = resizedBitmap.copy(Bitmap.Config.RGB_565, true);
            Canvas canvas = new Canvas(finalBitmap);

            Bitmap overlayBitmap = null;
            if (mWeaponType == wep1) {
                overlayBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.wep1);
            } else if (mWeaponType == wep2) {
                overlayBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.wep2); 
            }

            if (overlayBitmap != null) {

                matrix = new Matrix();
                matrix.postRotate(90);
                Bitmap resizedOverlay = Bitmap.createBitmap(overlayBitmap, 0, 0, overlayBitmap.getWidth(), overlayBitmap.getHeight(), matrix, true);
                canvas.drawBitmap(resizedOverlay, 0, 0, new Paint());
                canvas.scale(50, 0);
                canvas.save();
                //finalBitmap is the image with the overlay on it

                // rotate image to save in landscape mode
                matrix = new Matrix(); 
                matrix.postRotate(270); 
                finalBitmap = Bitmap.createBitmap(finalBitmap, 0, 0, finalBitmap.getWidth(), finalBitmap.getHeight(), matrix,
                        true);

                // convert final bitmap to byte array
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byteArray = stream.toByteArray();
            }
        }

        catch(OutOfMemoryError e) {
            //fail
        }

        try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(byteArray); 
                fos.close();

                // Notify system that SD card has been updated
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
                Log.i(TAG, "Picture saved, intent broadcast that SD has been updated");
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        } catch (NullPointerException e) {

        }

        finally {
            mCamera.startPreview();
            captureBtn.setVisibility(ImageButton.VISIBLE);
        }
    }
};

我得到NullPointerException,如下所示:

java.lang.NullPointerException
at java.io.FileOutputStream.write(FileOutputStream.java:256)
at com.mypackage.MyActivity.onPictureTaken(MyActivity.java:215)

 fos.write(byteArray); 

线。

上面列出的getOutputMediaFile方法如下:

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES);

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("HalfLife2 Booth", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

我认为问题可能出在某处,试图获取输出媒体文件。我已尝试更新要使用的应用:

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyApp");

相反,一个用户报告了要修复的问题,但有几个人报告说它仍然存在。

任何想法,任何人?当我无法重现或靠近它时,我发现这是一个棘手的调试/修复。

2 个答案:

答案 0 :(得分:2)

确保以原始格式缩放图片。我发现某些手机型号的原始图像很大。我建议缩放图像,如果它非常大,因为你将运行你的虚拟机内存不足。

示例:

     imageRef = sd_card_path+"/"+ImageName;
                BitmapFactory.Options resample = new BitmapFactory.Options();
                resample.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(imageRef, resample);
                int width = resample.outWidth;
                int height = resample.outHeight;
                if(width*height > utility.IMAGE_SIZE_MIN){
                    int imageSizef = (int)(width*height/utility.IMAGE_SIZE_MIN);
                    String imageSize = imageSizef+"";
                    resample.inSampleSize = Integer.parseInt(imageSize);
                    resample.inJustDecodeBounds = false;
                    image.setImageBitmap(BitmapFactory.decodeFile(imageRef , resample));                    
                }else{
                    image.setImageBitmap(BitmapFactory.decodeFile(imageRef ));
                }

答案 1 :(得分:1)

鉴于异常跟踪,它会建议您遇到OutOfMemorryError,因此byteArray永远不会被填充,因此您将null传递给fos.write方法