Search code examples
androidandroid-cameraandroid-camera-intent

Overcoming Photo Results from Android Cameras That Produce Low Resolution


I want to take a picture from the Camera and Upload it to Server, but when I take a Picture from the Camera and Upload the Picture is low Resolution.

Code :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CaptureImageFromCamera = (ImageView)findViewById(R.id.imageView);

    CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(takePictureIntent, 1);
            }
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 1)
            try {
                onCaptureImageResult(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

private void onCaptureImageResult(Intent data) throws IOException {
    bitmap = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes;
    bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            "DCA/Attachment/" + System.currentTimeMillis() + ".png");
    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ImageViewHolder.setImageBitmap(bitmap);
}

Can be seen that the image quality is low resolution.

Is there a way to solve this problem?

[UPDATE] HOW I TO SOLVE THIS

I read the article here to solve this problem

Note: Read from the start page


Solution

  • package com.example.admin.camsdemo;
    
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.provider.MediaStore;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends AppCompatActivity {
    Button captureimage;
    ContentValues cv;
    Uri imageUri;
    ImageView imgView;
        public static final int PICTURE_RESULT=111;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            captureimage=(Button)findViewById(R.id.opencamera);
            imgView=(ImageView)findViewById(R.id.img);
            captureimage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    cv = new ContentValues();
                    cv.put(MediaStore.Images.Media.TITLE, "My Picture");
                    cv.put(MediaStore.Images.Media.DESCRIPTION, "From Camera");
                    imageUri = getContentResolver().insert(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    startActivityForResult(intent, PICTURE_RESULT);
                }
            });
        }
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
    
                case PICTURE_RESULT:
                    if (requestCode == PICTURE_RESULT)
                        if (resultCode == Activity.RESULT_OK) {
                            try {
                              Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
                                        getContentResolver(), imageUri);
                                imgView.setImageBitmap(thumbnail);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
    
                        }
            }
        }
    }