0

My application got crashed on devices with no SD cards in it, but is working fine with devices which are having SD card in it.When i debugged it, i found that on

mCamera.takePicture(null, null, jpegCallBack);

Method app is getting crashed with above error.I goggled a lot but didn't found any solution , i saw this link :-

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

So is it possible to capture images in background service in device with no SD card in it.

Please provide me some clues Here are some methods of my hiddenCamera class

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

And also isSDPresent always returns me true value .

Please provide me your suggestions on this. I am really stuck at this point from last 2-3 days.

This is the issue of Device too as in Samsung Grand , my code is working fine even its not having SD card in it.But in Moto E its my application getting crashed.Camera settings plays an important role in it.

Thanks

sid
  • 1,116
  • 1
  • 10
  • 27

1 Answers1

0

Finally i am done with this, though i got busy in some other task but today i get time to post my Answer on this topic, As this topis is very general , so i am posting this answer inorder to help others who might have thought of this functionality , So i done this thing by using SurfaceTexture but it will only work for versions greater thet 4 and for versions less than 4 you need to use surfaceView.

So here is my code :-

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
}

}

Hope this will help others .......

Here are the links for reference :-

Example of Camera preview using SurfaceTexture in Android

Camera.takePicture throws RunTimeException

Cheers!!!!!

Community
  • 1
  • 1
sid
  • 1,116
  • 1
  • 10
  • 27