首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >camera.takepicture在某些设备上失败,如moto e

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

Stack Overflow用户
提问于 2015-12-04 14:54:26
回答 1查看 624关注 0票数 0

我的应用程序在没有SD卡的设备上崩溃了,但在it.When中有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类的一些方法

代码语言:javascript
复制
@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;
    }
};

而且isSDPresent总是给我返回真值。

请给我提供您对此的建议。在过去的2-3天里,我真的被困在这一点上。

这是设备的问题,因为在三星大,我的代码工作得很好,即使它没有SD卡在it.But的Moto E它的应用程序获取crashed.Camera设置是一个重要的角色。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-12-21 19:48:31

最后我完成了这个,虽然我忙于一些其他的任务,但今天我有时间在这个主题上发布我的答案,因为这个主题非常一般,所以我发布这个答案是为了帮助其他可能想到这个功能的人,所以我使用SurfaceTexture做了这个事情,但它只适用于t4以上的版本,并且对于低于4的版本,你需要使用surfaceView。

所以这是我的代码:

代码语言:javascript
复制
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

干杯!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34082556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档