简体   繁体   中英

Getting Blur image in onActivityResult() and How to get good quality Captured image in onActivityResult()

After clicking camera, if RESULT is OK then it should ask user to enter the file name in an alert dialogue and store it in particular folder. But the image is getting blur.

 if (rc == PackageManager.PERMISSION_GRANTED) {
        Intent in = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(in, 0);
    }}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0 && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        SaveImage(imageBitmap);
    }}


public void SaveImage(Bitmap showedImgae){
    final AlertDialog.Builder builder = new AlertDialog.Builder(CaptureActivity.this, R.style.MyDialogTheme);
    LayoutInflater layoutInflater = getLayoutInflater();
    final View view1 = layoutInflater.inflate(R.layout.filenamepomfret, null);
    EditText pomname=view1.findViewById(R.id.pomname);
    pomname.setText(getFileName());
    builder.setCancelable(true);
    builder.setTitle("Enter File Name");
    builder.setPositiveButton("Save", (dialog, which) -> {

    });
    builder.setNegativeButton("Cancel", (dialog, which) ->
            dialog.dismiss()
    );
    builder.setView(view1);
    final AlertDialog dialog = builder.create();
    dialog.show();
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
        Boolean wantToCloseDialog = (pomname.getText().toString().trim().isEmpty());
        Toast.makeText(this, "Enter Name", Toast.LENGTH_SHORT).show();
        if (!wantToCloseDialog) {
            dialog.dismiss();
            File myfolder = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)));
            myfolder.mkdirs();
            String fname = pomname.getText().toString();
            File imageFile = new File(myfolder, fname);
            if (imageFile.exists()) imageFile.delete();
            try {
                FileOutputStream out = new FileOutputStream(imageFile);
                showedImgae.compress(Bitmap.CompressFormat.JPEG, 100, out);
                Toast.makeText(CaptureActivity.this, "Image Saved", Toast.LENGTH_SHORT).show();
                out.flush();
                out.close();
                dialog.dismiss();
                finish();

            } catch (Exception e) {
                dialog.dismiss();
                e.printStackTrace();

                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                  Uri contentUri = Uri.fromFile(imageFile);
              mediaScanIntent.setData(contentUri);
                getApplicationContext().sendBroadcast(mediaScanIntent);
            }

        }  });
    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(v -> {
        dialog.dismiss();
        finish();
    });

before onCreate method:

private static final int IMAGE_PICK_CAMERA_CODE = 1001;
Uri image_uri

Call this method to pick image from camera:

public void pickCamera() {
    //take image from default camera
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "New Picture");
    values.put(MediaStore.Images.Media.DESCRIPTION, "From Camera");
    image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
    startActivityForResult(cameraIntent, IMAGE_PICK_CAMERA_CODE);
}

now on activity result:

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

        if (resultCode == RESULT_OK){

            if (requestCode == IMAGE_PICK_CAMERA_CODE){
                //set image to image view
                mImageView.setImageURI(image_uri);
            }
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM