【问题标题】:The photo lose its quality when it appears into the ImageView照片出现在 ImageView 中时会失去其质量
【发布时间】:2017-02-19 16:54:22
【问题描述】:

如有语法错误,请见谅。

我制作了一个允许您拍照的应用程序,在您单击“确定”后,图片会出现在 ImageView 中。 现在,我不知道为什么,当我在我的 Nexus 5X 上尝试这个应用程序时,照片出现在 ImageView 中时会失去质量。 应用图片(图片查看): 摄像头图片:

片段代码:

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

}

【问题讨论】:

  • 我添加了一个新答案。这次我运行了代码,它运行良好。你可以看看这个。

标签: android camera photo


【解决方案1】:

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

【讨论】:

  • 我阅读了教程并尝试做这样的事情:pastebin.com/WRAzR71V 但我正在处理一个片段,所以我遇到了一些错误,请阅读粘贴以获取更​​多信息
  • 使用getActivity() 而不是this
【解决方案2】:

只需复制粘贴整个班级:

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

【讨论】:

  • tahsinRupam,对不起,如果我之前没有回答你(我打破了电脑)。无论如何,我的另一个应用程序有问题,如果你能帮助我,那就太好了。我在这里与您联系是因为我不知道另一种方法,如果这不是一个好方法,请原谅。 stackoverflow.com/questions/43237864/…
  • 没关系。我会尽力帮助你的。
  • 抱歉,我对这类应用没有经验。尽管如此,我还是会收集一些知识并尝试为您提供有关该问题的任何解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
相关资源
最近更新 更多