从未显示在 ImageView 中的图库或相机照片中选择

Selected from gallery or camera photos not displaying in the ImageView

Camera 拍摄的照片或选择的照片未显示在 ImageView 中。我正在使用以下代码:

private void attachFile() {
    final CharSequence[] items = {"Camera", "Gallery", "Cancel"};

    AlertDialog.Builder builder;
    builder = new AlertDialog.Builder(mContext);
    builder.setTitle("Add Image");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if(items[i].equals("Camera")) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, REQUEST_CAMERA);

            } else if (items[i].equals("Gallery")) {

                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent,"Select Photo"), SELECT_FILE);

            } else if (items[i].equals("Cancel")) {
                dialogInterface.dismiss();
            }
        }
    });
    builder.show();
}

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


    if (requestCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA && data != null) {
            Log.d(TAG, "onActivityResult: done take photo");
            Uri mImageCaptureUri = data.getData();
            imageView.setImageURI(mImageCaptureUri);
            /*Bundle bundle = data.getExtras();
            final Bitmap bitmap = (Bitmap) bundle.get("data");
            mOnphotoSelectedListener.getImageBitmap(bitmap);*/

        } else if (requestCode == SELECT_FILE) {

            Uri selectImageUri = data.getData();
            mOnphotoSelectedListener.getImagePath(selectImageUri);

            //imageView.setImageURI(selectImageUri);
            //Picasso.with(mContext).load(selectImageUri).into(imageView);
            //Picasso.with(this).load(selectImageUri).into(imageView);
        }
    }
}

onActivityResult

中使用了下面的代码
if (requestCode == Activity.RESULT_OK) {
        File user_dp;

        if (requestCode == REQUEST_CAMERA && data != null) {

            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
            File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
            FileOutputStream fo;
            try {
                destination.createNewFile();
                fo = new FileOutputStream(destination);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            user_dp = destination;

            //update_profilepic is ImageView reference
            Picasso.with(getContext()).load(user_dp).into(update_profilepic);


        } else if (requestCode == SELECT_FILE) {
            Bitmap bm = null;
            if (data != null) {
                try {
                    bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //ivImage is image reference
            ivImage.setImageBitmap(bm);

        }
    }
}

在 if 语句的第一行,我没有使用 resultCode,而是使用了 requestCode。

我有这段代码,希望它能正常工作 我为 ImageProfile 设置了 onclick,我给用户图片 + 裁剪 我希望它对你有用

    private void mImage_profileOnclick() {

    int persmissonCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
    if (persmissonCheck == PackageManager.PERMISSION_DENIED) {
        Req();
    }else {
        Dialog dialog = new Dialog(getActivity());
        dialog.setContentView(R.layout.dialog_open_device);
        Button pic, gal;
        pic = (Button) dialog.findViewById(R.id.pick);
        gal = (Button) dialog.findViewById(R.id.gallery);

        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(takePicture, 0);
                dialog.cancel();
                dialog.dismiss();
            }
        });

        gal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(pickPhoto, 1);
                dialog.cancel();
                dialog.dismiss();
            }
        });
        //dialog.create();
        dialog.show();
    }
}
       @Override
       public void onActivityResult(int requestCode, int resultCode, Intent 
       imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
         switch (requestCode) {
        case 0:

            if (resultCode == RESULT_OK) {
       Bundle extras = imageReturnedIntent.getExtras();
    Bitmap imageBitmap = (Bitmap) extras.get("data");
    Dialog dialog = new Dialog(getActivity());
                dialog.setContentView(R.layout.cropeimage);
                CropImageView cropImageView = (CropImageView) dialog.findViewById(R.id.cropImageView);
                Button button = (Button) dialog.findViewById(R.id.bac);
                cropImageView.setImageBitmap(imageBitmap);

                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                  mImage_profile.setImageBitmap(cropImageView.getCroppedImage());
                        dialog.cancel();
                        dialog.dismiss();
                    }
                });
                //dialog.create();
                dialog.show();
            }
            break;
        case 1:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = imageReturnedIntent.getData();
                Dialog dialogs = new Dialog(getActivity());
                dialogs.setContentView(R.layout.cropeimage);
                CropImageView cropImageView = (CropImageView) dialogs.findViewById(R.id.cropImageView);
                cropImageView.setImageUriAsync(selectedImage);
                Button button = (Button) dialogs.findViewById(R.id.bac);

                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bitmap cropped = cropImageView.getCroppedImage();
                        mImage_profile.setImageBitmap(cropped);
                        dialogs.cancel();
                        dialogs.dismiss();
                    }
                });
                //dialogs.create();
                dialogs.show();
            }
            break;
    }
}

//@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void Req() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {
        Toast.makeText(getActivity(), "شما مجوز استفاده از دوربین یا هر چیز دیگر را به برنامه نداده اید", Toast.LENGTH_SHORT).show();

    }
    else {
        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 1);
        mImage_profileOnclick();
    }   }