camera.takepicture在某些设备上失败,例如moto e

时间:2015-12-04 06:54:26

标签: android android-camera

我的应用程序在没有SD卡的设备上崩溃了,但是对于装有SD卡的设备工作正常。当我调试它时,我发现它在

mCamera.takePicture(null, null, jpegCallBack);

方法应用程序正在崩溃以上错误。我很多但是没有找到任何解决方案,我看到了这个链接: -

http://forums.androidcentral.com/motorola-droid-x/102987-camera-won-t-take-pictures-without-sd-card.html

因此可以在没有SD卡的设备中捕获后台服务中的图像。

请提供一些线索 以下是我的hiddenCamera类的一些方法

@SuppressWarnings("deprecation")
private void startCapturingCall() {
    final Boolean isSDPresent = android.os.Environment
            .getExternalStorageState().equals(
                    android.os.Environment.MEDIA_MOUNTED);
    if (mCamera != null) {
        parameters = mCamera.getParameters();
        if (FLASH_MODE == null || FLASH_MODE.isEmpty()) {
            FLASH_MODE = "auto";
        }
        parameters.setFlashMode(FLASH_MODE);
        pictureSize = getBiggesttPictureSize(parameters);
        if (pictureSize != null)
            parameters
                    .setPictureSize(pictureSize.width, pictureSize.height);
        // set camera parameters
        mCamera.setParameters(parameters);

        mCamera.startPreview();
        new Handler().postDelayed(new Runnable() {

            @SuppressWarnings("deprecation")
            @Override
            public void run() {
                if (isSDPresent) {
                    mCamera.takePicture(null, null, jpegCallBack);
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Please Insert SD card", 1000).show();
                }

            }
        }, 2000);

    }

}

@SuppressWarnings("deprecation")
Camera.PictureCallback jpegCallBack = new Camera.PictureCallback() {

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

        Boolean isSDPresent = android.os.Environment
                .getExternalStorageState().equals(
                        android.os.Environment.MEDIA_MOUNTED);

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());

        // checking for SD card
        if (isSDPresent) {
            mediaStorageDir = new File(Environment
                    .getExternalStorageDirectory().getAbsolutePath(),
                    IMAGE_DIRECTORY_NAME);

            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");

            // Create the storage directory if it does not exist
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                }
            }

            try {
                Bitmap userImage = BitmapFactory.decodeByteArray(data, 0,
                        data.length);

                // set file out stream
                FileOutputStream out = new FileOutputStream(mediaFile);
                // set compress format quality and stream
                userImage.compress(Bitmap.CompressFormat.JPEG, 50, out);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                userImage.compress(Bitmap.CompressFormat.JPEG, 50, baos);

                mByteArray = baos.toByteArray();

                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            Toast.makeText(getApplicationContext(),
                    "Please insert SD card !", Toast.LENGTH_LONG).show();

        }
        if (mediaStorageDir.exists()) {
            getPathOfCapturedImage();
        }
        HiddenCamera.this.finish();
        CameraService.IS_ACTIVITY_FINISHED = true;
    }
};

而且,SDDresent总是会给我带来真正的价值。

请就此向我提出建议。在过去的2-3天里,我真的陷入了困境。

这也是三星Grand的设备问题,我的代码工作正常,即使它没有SD卡。但在Moto E中我的应用程序崩溃。相机设置在其中起着重要作用。

由于

1 个答案:

答案 0 :(得分:0)

最后我完成了这个,虽然我忙于其他任务,但今天我有时间在这个主题上发表我的答案,因为这个topis很一般,所以我发布这个答案,以帮助其他人我已经想到了这个功能,所以我通过使用 SurfaceTexture 来完成这个任务,但它只适用于版本大于4的版本,而对于小于4的版本,你需要使用surfaceView。

所以这是我的代码: -

public class SurfaceTextureActivity extends Activity implements
    SurfaceTextureListener {
private Parameters mParameters;
private Camera.Size mPictureSize;
private static final String sIMAGE_DIRECTORY_NAME = "HiddenCapturedPics";
private byte[] mByteArray;
private Camera mCamera;
private TextureView mTextureView;
private File mMediaFile, mMediaStorageDir = null;
private String mEncodedImage, mImageName, mFinalResponse,
        mFlashMode;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mTextureView = new TextureView(this);
    setContentView(mTextureView);   
    if (checkCameraHardware(getApplicationContext())) {
        mTextureView.setSurfaceTextureListener(this);
        Bundle extras = getIntent().getExtras();
        mFlashMode = extras.getString("FLASH");
    } else {
        Toast.makeText(getApplicationContext(),
                "Your Device dosen't have a Camera !", Toast.LENGTH_LONG)
                .show();
    }
}
/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        return true;
    } else {
        return false;
    }
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
        int height) {
    mCamera = Camera.open();
    mTextureView.setLayoutParams(new FrameLayout.LayoutParams(0, 0,
            Gravity.CENTER));
    try {
        mCamera.setPreviewTexture(surface);
    } catch (IOException t) {
    }
    mCamera.startPreview();
    startCapturingCall();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
        int height) {
    // Ignored, the Camera does all the work for us
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    mCamera.stopPreview();
    mCamera.release();
    return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    Toast.makeText(getApplicationContext(), "Dfg", Toast.LENGTH_SHORT)
            .show();
    // Update your view here!
}
Camera.PictureCallback jpegCallBack = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Boolean isSDPresent = android.os.Environment
                .getExternalStorageState().equals(
                        android.os.Environment.MEDIA_MOUNTED);
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        // checking for SD card
        if (isSDPresent) {
            mMediaStorageDir = new File(Environment
                    .getExternalStorageDirectory().getAbsolutePath(),
                    sIMAGE_DIRECTORY_NAME);
            mMediaFile = new File(mMediaStorageDir.getPath()
                    + File.separator + "IMG_" + timeStamp + ".jpg");
            if (!mMediaStorageDir.exists()) {
                if (!mMediaStorageDir.mkdirs()) {
                }
            }
            try {
                final BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                Bitmap userImage = BitmapFactory.decodeByteArray(data, 0,
                        data.length, options);
                FileOutputStream out = new FileOutputStream(mMediaFile);
                userImage.compress(Bitmap.CompressFormat.JPEG, 50, out);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                userImage.compress(Bitmap.CompressFormat.JPEG, 50, baos);
                mByteArray = baos.toByteArray();
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(getApplicationContext(),
                    "Please insert SD card !", Toast.LENGTH_LONG).show();
        }
        if (mMediaStorageDir.exists()) {
           getPathOfCapturedImage();
        }
        SurfaceTextureActivity.this.finish();
        CameraService.IS_ACTIVITY_FINISHED = true;
    }
};

private void startCapturingCall() {
    if (mCamera != null) {
        mParameters = mCamera.getParameters();
        if (mFlashMode == null || mFlashMode.isEmpty()) {
            mFlashMode = "auto";
        }
        mParameters.setFlashMode(mFlashMode);
        mPictureSize = getBiggesttPictureSize(mParameters);
        if (mPictureSize != null)
            mParameters.setPictureSize(mPictureSize.width,
                    mPictureSize.height);
        mCamera.setParameters(mParameters);
        mCamera.startPreview();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mCamera != null) {
                    mCamera.startPreview();
                    mCamera.takePicture(null, null, jpegCallBack);
                } else {
                    mCamera = getCameraInstance();
                    mCamera.startPreview();
                    mCamera.takePicture(null, null, jpegCallBack);
                }
            }
        }, 2000);
    }
}
private Camera.Size getBiggesttPictureSize(Camera.Parameters parameters) {
    Camera.Size result = null;
    for (Camera.Size size : parameters.getSupportedPictureSizes()) {
        if (result == null) {
            result = size;
        } else {
            int resultArea = result.width * result.height;
            int newArea = size.width * size.height;
            if (newArea > resultArea) {
                result = size;
            }
        }
    }
    return (result);
}
public static Camera getCameraInstance() {
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

}

希望这会有助于其他人.......

以下是供参考的链接: -

Example of Camera preview using SurfaceTexture in Android

Camera.takePicture throws RunTimeException

干杯!!!!!