当照片出现在 ImageView 中时,它会失去质量

The photo lose its quality when it appears into the ImageView

如有任何语法错误,请原谅。

我制作了一个允许您拍照的应用程序,在您单击 "Ok" 后,图片出现在 ImageView 中。 现在,我不知道为什么,当我在我的 Nexus 5X 上尝试这个应用程序时,照片出现在 ImageView 中时质量下降。 应用图片(Image View): 相机图像:

片段代码:

public class CameraFragment extends Fragment{

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    dispatchTakePictureIntent();
    return inflater.inflate(R.layout.fragment_camera,container,false);
}

ImageView SkimmedImageImg;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    SkimmedImageImg = (ImageView)view.findViewById(R.id.SkimmedImg);
}

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Fragment CameraFragment = this;
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    CameraFragment.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}

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

    if(resultCode == Activity.RESULT_OK){
        if(requestCode == REQUEST_IMAGE_CAPTURE){
            Bitmap SkimmedImgData = (Bitmap) data.getExtras().get("data");
            SkimmedImageImg.setImageBitmap(SkimmedImgData);
        }
    }
}

}

当您调用 Bitmap SkimmedImgData = (Bitmap) data.getExtras().get("data");,然后使用 SkimmedImageImg.setImageBitmap(SkimmedImgData); 设置图像时,您只是设置了所拍摄图像的缩略图,这就是质量如此失真的原因。您可以按照 this 教程,其中将向您展示如何保存全尺寸图像,请查看 header 保存 Full-size 照片

复制粘贴整个class:

public class CameraFragment extends android.support.v4.app.Fragment{   

    String mCurrentPhotoPath;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final int MyVersion = Build.VERSION.SDK_INT;
        if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (!checkIfAlreadyhavePermission_new()) {
                requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
            } else {
                if (!checkIfAlreadyhavePermission()) {
                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 2);
                } else {
                    try {
                        dispatchTakePictureIntent();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            try {
                dispatchTakePictureIntent();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return inflater.inflate(R.layout.fragment_camera,container,false);
    }

    ImageView SkimmedImageImg;
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SkimmedImageImg = (ImageView)view.findViewById(R.id.SkimmedImg);
    }

    static final int REQUEST_IMAGE_CAPTURE = 1;

    private void dispatchTakePictureIntent() throws IOException {
        CameraFragment cameraFragment = this;
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                return;
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(getActivity(),
                        BuildConfig.APPLICATION_ID + ".provider",
                        createImageFile());;
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                cameraFragment.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }

    }

    private boolean checkIfAlreadyhavePermission() {
        int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    private boolean checkIfAlreadyhavePermission_new() {
        int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
        return result == PackageManager.PERMISSION_GRANTED;
    }

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

        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_IMAGE_CAPTURE) {
                Uri imageUri = Uri.parse(mCurrentPhotoPath);
                File file = new File(imageUri.getPath());
                Glide.with(getActivity())
                     .load(file)
                     .into(SkimmedImageImg);

                // ScanFile so it will be appeared on Gallery
                MediaScannerConnection.scanFile(getActivity(),
                        new String[]{imageUri.getPath()}, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                            public void onScanCompleted(String path, Uri uri) {
                            }
                        });
            }
        }

    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM), "Camera");
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }


    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (!checkIfAlreadyhavePermission()) {
                        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 2);
                    } else {
                        try {
                            dispatchTakePictureIntent();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    Toast.makeText(getActivity(), "NEED CAMERA PERMISSION", Toast.LENGTH_LONG).show();
                }
                break;
            }

            case 2: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    try {
                        dispatchTakePictureIntent();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(getActivity(), "NEED STORAGE PERMISSION", Toast.LENGTH_LONG).show();
                }
                break;
            }
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

}

如果在 MainActivity 中加载 Fragment 时出现错误,只需使用 getSupportFragmentManager():

 FragmentManager fm = getSupportFragmentManager();