在onActivityResult()中获取模糊图像以及如何在onActivityResult()中获得高质量捕获的图像

时间:2018-06-08 07:35:57

标签: java android

单击相机后,如果RESULT为OK,则应要求用户在警告对话框中输入文件名并将其存储在特定文件夹中。 但是图像越来越模糊了。

 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();
    });

1 个答案:

答案 0 :(得分:0)

在onCreate方法之前:

private static final int IMAGE_PICK_CAMERA_CODE = 1001;
Uri image_uri

调用此方法从相机中拾取图像:

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);
}

现在进入活动结果:

@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);
            }
        }

    }