How to capture the image and save it in sd card

吃可爱长大的小学妹 提交于 2019-11-28 09:34:00

I know this isn't exactly an answer to your question, but wouldn'nt it be easier to use the stock camera application? You can access it using this code in your activity:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.capture);

    Button capture = (Button) findViewById(R.id.capture_button);
    capture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // We use the stock camera app to take a photo
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
            startActivityForResult(intent, TAKE_PHOTO_CODE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        Uri imagePath = getImageUri();

        doSomething();
    }
}

/**
 * Get the uri of the captured file
 * @return A Uri which path is the path of an image file, stored on the dcim folder
 */
private Uri getImageUri() {
    // Store image in dcim
    File file = new File(Environment.getExternalStorageDirectory() + "/DCIM", CAPTURE_TITLE);
    Uri imgUri = Uri.fromFile(file);

    return imgUri;
}
Dave Cote

Just a suggestion, use EXIF on jpg before uploading it to the server. I found uploading images tediously slow because of the quality of todays smartphone cameras. A simple solution is using an exif reading program to extract the thumbnail of the jpeg, save said thumbnail as a new jpeg, and upload that. It's identical to the original photo yet much smaller (under 100kb). I am not sure if it's image quality you want, but if not, and to upload lots of pics, use the exif method. I programmed on python sl4a and used EXIF.py, but I'm sure there's similar in Java.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!