在我的Android应用程序中拍摄的照片似乎并不清楚

时间:2017-03-12 11:53:02

标签: android

我拍摄照片,我拍的照片来到这张imageview.However不清楚。我怎么能解决这个问题?

enter image description here

我的代码如下

private void GalleryOpen() {
        GalIntent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(Intent.createChooser(GalIntent,"Select Image from Gallery"),2);
    }

    private void CameraOpen() {
        CamIntent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        file=new File(Environment.getExternalStorageDirectory(),"file"+String.valueOf(System.currentTimeMillis())+".jpg");
        uri=Uri.fromFile(file);
        CamIntent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
        CamIntent.putExtra("return-data",true);
        startActivityForResult(CamIntent,0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 0 && resultCode == RESULT_OK)
            CropImage();
        else if(requestCode == 2){
            if(data!= null){
                uri=data.getData();
                CropImage();
            }
        }
        else if(requestCode == 1){
            if(data !=null){
                Bundle bundle=data.getExtras();
                Bitmap bitmap=bundle.getParcelable("data");
                imageView.setImageBitmap(bitmap);

            }
        }
    }

    private void CropImage() {
        try{
            CropIntent =new Intent("com.android.camera.action.CROP");
            CropIntent.setDataAndType(uri,"image/*");
            CropIntent.putExtra("crop","true");
            CropIntent.putExtra("outputX",180);
            CropIntent.putExtra("outputY",180);
            CropIntent.putExtra("aspectX",3);
            CropIntent.putExtra("aspectY",4);
            CropIntent.putExtra("scaleUpIfNeeded",true);
            CropIntent.putExtra("return-data",true);
            startActivityForResult(CropIntent,1);
        }
        catch (AccessControlException ex){

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case RequestPermissionCode:{
                if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
                    Toast.makeText(this,"Permission Granted",Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(this,"Permission Canceled",Toast.LENGTH_SHORT).show();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个

Uri selectedImage;
private Uri imageUri;
String path;
ImageView imageView; 

  button.setOnClickListener(new View.OnClickListener()
  {
       public void onClick(View v)
        {
            activeTakePhoto(); // open camera
         }
    });

    private void activeTakePhoto() {
    String filename = "Pic_" + System.currentTimeMillis() + ".jpg";
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(), filename);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
    Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
     }

    @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           super.onActivityResult(requestCode, resultCode, data);
           switch (requestCode) {
           case RESULT_LOAD_IMAGE:
            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
                  selectedImage = data.getData();
                  imageView.setImageURI(selectedImage);
                }
                    break;

             case REQUEST_IMAGE_CAPTURE:
               if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                  try {
                        selectedImage = imageUri;
                        getContentResolver().notifyChange(selectedImage,null);
                         imageView.setImageURI(null);
                         imageView.setImageURI(imageUri);
                         } catch (Exception e) {
                          Toast.makeText(this, "Failed to load",Toast.LENGTH_SHORT).show();
                         Log.e("Camera", e.toString());
                            }
                         Log.e("A", "AAA");
                        }
                }
            }