0

While I am using a camera it returs low quality image. In ImageView I can see a good quality, but I don't know why it returns a bad quality.
This is a piece of by code:

public class MainActivity extends AppCompatActivity {

    private static final int CAM_REQUEST =2 ;
    Button button;
    ImageView imageView;
    Paint mPaint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);

        button=(Button)findViewById(R.id.btcam);
        imageView=(ImageView)findViewById(R.id.img);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent camera_intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if(camera_intent.resolveActivity(getPackageManager())!=null)
                    {
                        startActivityForResult(camera_intent,CAM_REQUEST);
                    }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {


        if(requestCode==CAM_REQUEST && resultCode==RESULT_OK)
        {
            Bundle extras =data.getExtras();
            Bitmap imagebitmap=(Bitmap)extras.get("data");
            imageView.setImageBitmap(imagebitmap);
        }


    }
}

Thank you for help

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
zaid
  • 1
  • 3
  • 1
    `Intent data` only includes a snapshot image and possibly not even that on some devices. You need to get the captured image from the gallery (or from the filesystem if you would have specified a file path for the image). – Markus Kauppinen Sep 24 '19 at 11:29
  • Thanks Markus . i am new to this can u tell me how it can be done ? – zaid Sep 24 '19 at 11:32

1 Answers1

2

Basically in onActivityResult(), this Bitmap imagebitmap=(Bitmap)extras.get("data"); just return the thumbnail of the image. For full-sized camera image you should point the camera to save image in temporary file, like:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo;
    try
    {
        // place where to store camera taken picture
        photo = this.createTemporaryFile("picture", ".jpg");
        photo.delete();
    }
    catch(Exception e)
    {
        Log.v(TAG, "Can't create file to take picture!");
        Toast.makeText(activity, "Please check SD card! Image shot is impossible!", 10000);
        return false;
    }
    mImageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    //start camera intent
    activity.startActivityForResult(this, intent, MenuShootImage);

Here is how to create a temporary file.

private File createTemporaryFile(String part, String ext) throws Exception
{
    File tempDir= Environment.getExternalStorageDirectory();
    tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
    if(!tempDir.exists())
    {
        tempDir.mkdirs();
    }
    return File.createTempFile(part, ext, tempDir);
}

Then after image capture intent finished to work - just grab your picture from imageUri using following code:

public void grabImage(ImageView imageView)
{
    this.getContentResolver().notifyChange(mImageUri, null);
    ContentResolver cr = this.getContentResolver();
    Bitmap bitmap;
    try
    {
        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
        imageView.setImageBitmap(bitmap);
    }
    catch (Exception e)
    {
        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "Failed to load", e);
    }
}


//called after camera intent finished
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    //MenuShootImage is user defined menu option to shoot image
    if(requestCode==MenuShootImage && resultCode==RESULT_OK) 
    {
       ImageView imageView;
       //... some code to inflate/create/find appropriate ImageView to place grabbed image
       this.grabImage(imageView);
    }
    super.onActivityResult(requestCode, resultCode, intent);
}

Just formate the code Happy coding...

Manoj Kumar
  • 332
  • 1
  • 7