我有一个Android应用程序,它在某些手机上崩溃了,而在其他手机上却崩溃了。它的下载量约为5,000,到目前为止,已收到约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");


相反,一位用户报告了该问题已得到解决,但几位用户报告了该问题仍然存在。

有什么想法吗?当我无法重现或接近它时,我发现这是一个调试/修复的棘手问题。

最佳答案

确保以原始格式缩放图片。我发现某些手机型号上的原始图像很大。如果图像很大,我建议缩放图像,因为这样会使虚拟机的内存不足。

例:

     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 ));
                }

10-06 11:34