How to capture the image and save it in sd card

后端 未结 2 879
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 23:59

hey guys i am using the following code to access camera from my application. The application is able to access the camera i have also added a button whose onclicklistener ad

相关标签:
2条回答
  • 2020-12-10 00:36

    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;
    }
    
    0 讨论(0)
  • 2020-12-10 00:43

    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.

    0 讨论(0)
提交回复
热议问题