How can I specify the taken image quality on Andro

2019-08-19 09:16发布

问题:

This question already has an answer here:

  • How to make Android Camera Intent return full size picture 2 answers

I found these two part of code, to how to take a photo from the camera in Android:

Inside the onCreate() method:

        Button capture;

        capture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
            }
        });

And

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

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");

    }
}

And it works, but the problem is that, the quality of the result image is very low! I like to know how can I specify what quality of image I want to take?

Also like to know, are there other options that bitmap to store or work on images?

EDIT:

This is what I see before pushing the capture button:

And this is what it takes after pushing the capture button(it reduces the quality):

I must say, when I take photos with my phone(outside of this app I mean) it works good, but inside the my written app, it reduces the taken image quality!

**Also I have another question...how can I remove this second page that shows after it took the image(the page shows RETRY-OK options I mean).

回答1:

Check below solution for your problem.

 public static File IMAGE_PATH = null;
     public static final int CAMERA_REQUEST = 100;
         private void openCameraApp(Context mContext) {
            Intent picIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);


            String file_path = Environment.getExternalStorageDirectory().toString() +
                    "/" + mContext.getResources().getString(R.string.app_name);

            File dir = new File(file_path);
            if (!dir.exists())
                dir.mkdirs();
            IMAGE_PATH = new File(dir, mContext.getResources().getString(R.string.app_name) +  System.currentTimeMillis() + ".png");


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                picIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(mContext, mContext.getPackageName()+".fileprovider", IMAGE_PATH));
            }
            else {
                picIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(IMAGE_PATH));
            }

            ((Activity) mContext).startActivityForResult(picIntent, CAMERA_REQUEST);

        }