当照片出现在ImageView中时,照片质量下降 [英] The photo lose its quality when it appears into the ImageView

查看:75
本文介绍了当照片出现在ImageView中时,照片质量下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于任何语法错误,请原谅.

excuse me for any grammatical errors.

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

I made an application that allow you to take a picture and after you clicked "Ok", the picture appear in an ImageView. Now, I don't know why, when I try this application on my Nexus 5X, the photo lose the quality when it appears into the ImageView. Application image (Image View): Camera Image:

片段代码:

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

}

推荐答案

只需复制粘贴整个类:

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中加载片段时遇到错误,只需使用getSupportFragmentManager():

If you are getting error while Fragment loading in MainActivity, just use getSupportFragmentManager():

 FragmentManager fm = getSupportFragmentManager();

这篇关于当照片出现在ImageView中时,照片质量下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆