Android studio 我能';不要在缩略图中显示捕获的图像

Android studio 我能';不要在缩略图中显示捕获的图像,android-studio,Android Studio,这是一项课堂作业,我今天必须完成。 因此,基本上捕获的图像已保存,但它不会显示在imgVpic imageview相机打开中,捕获的图像已正确保存并使用正确的文件名,但它不会显示在我设置的缩略图中,我在图库中进行了检查,图片再次出现。。图片不会显示在这个imgVpic图片视图中(我不断重复这一点,因为我的帖子大多是代码,没有“更多细节”我无法发布这个问题,请帮助我是一名学生,我讨厌我的老师) 这是我使用的代码 公共类CaptureImgaActivity扩展了AppCompatActivity{

这是一项课堂作业,我今天必须完成。 因此,基本上捕获的图像已保存,但它不会显示在imgVpic imageview相机打开中,捕获的图像已正确保存并使用正确的文件名,但它不会显示在我设置的缩略图中,我在图库中进行了检查,图片再次出现。。图片不会显示在这个imgVpic图片视图中(我不断重复这一点,因为我的帖子大多是代码,没有“更多细节”我无法发布这个问题,请帮助我是一名学生,我讨厌我的老师)

这是我使用的代码

公共类CaptureImgaActivity扩展了AppCompatActivity{

final int CAMERA_RESULT=0;
Uri imgUri=null;
Button btTakePic;
ImageView imgVPic;

private void CaptureImageUri(){
        String dir= Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM).getAbsolutePath();
        File folder=new File(dir + File.separator+"test");
        boolean success=true;

        if (!folder.exists()){
            try {
                success=folder.mkdir();
                Toast.makeText(this,"Folder Create Successfull", Toast.LENGTH_SHORT).show();
            }catch (Exception e){
                success = !success;
                e.printStackTrace();
            }
        }

    if (success){
        dir=dir+ File.separator+"test";
    }

    Calendar calendar=Calendar.getInstance();

    File file= new File(dir,"test"+(calendar.getTimeInMillis()+".jpg"));
    if (!file.exists()){
        try {
            file.createNewFile();
            imgUri= FileProvider.getUriForFile(CaptureImgActivity.this,BuildConfig.APPLICATION_ID,file);
            Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,imgUri);
            startActivityForResult(intent,CAMERA_RESULT);
        }catch (Exception e){
            e.printStackTrace();
        }
    }else {
        try {

            file.delete();

            file.createNewFile();

            imgUri= FileProvider.getUriForFile(CaptureImgActivity.this,BuildConfig.APPLICATION_ID,file);
            Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,imgUri);
            startActivityForResult(intent,CAMERA_RESULT);
        }catch (Exception e){
            e.printStackTrace();
        }
        }
    }

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == CAMERA_RESULT && requestCode == RESULT_OK){
        try {
                Uri imgUri=data.getData();
                Bitmap bitmap= MediaStore.Images.Media.getBitmap(this.getContentResolver(),imgUri);
                imgVPic.setImageBitmap(bitmap);
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_capture_img);

    btTakePic = (Button) findViewById(R.id.btTakePic);
    imgVPic = (ImageView) findViewById(R.id.imgVPic);

    btTakePic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CaptureImageUri();
        }
    });

}

}

我尝试了两种方法,比如从图库中选择图像和通过添加

android:requestLegacyExternalStorage="true"
我的代码与你的不同,但工作过程相同,但这对我来说是可行的

Uri imgUri=null;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
private Intent pictureActionIntent = null;
Bitmap bitmap;
String selectedImagePath;


private void SelectImage() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(
            getActivity());
    myAlertDialog.setTitle("Upload Pictures Option");
    myAlertDialog.setMessage("How do you want to set your picture?");

    myAlertDialog.setPositiveButton("Gallery",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent pictureActionIntent = null;

                    pictureActionIntent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(
                            pictureActionIntent,
                            GALLERY_PICTURE);

                }
            });

    myAlertDialog.setNegativeButton("Camera",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File file = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                    imgUri= FileProvider.getUriForFile(Objects.requireNonNull(getContext()), BuildConfig.APPLICATION_ID + ".provider", file);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);

                    startActivityForResult(intent, CAMERA_REQUEST);

                }
            });
    myAlertDialog.show();
}
OnActivityResult

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

    bitmap = null;
    selectedImagePath = null;

    if (resultCode == RESULT_OK && requestCode == CAMERA_REQUEST) {

        File f = new File(Environment.getExternalStorageDirectory()
                .toString());
        for (File temp : f.listFiles()) {
            if (temp.getName().equals("temp.jpg")) {
                f = temp;
                break;
            }
        }

        if (!f.exists()) {
            Toast.makeText(getContext(), "Error while capturing image", Toast.LENGTH_LONG).show();
            return;
        }

        try {
            bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
            bitmap = Bitmap.createScaledBitmap(bitmap, 400, 400, true);
            int rotate = 0;
            try {
                ExifInterface exif = new ExifInterface(f.getAbsolutePath());
                int orientation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);
                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotate = 270;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotate = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotate = 90;
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            Matrix matrix = new Matrix();
            matrix.postRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), matrix, true);

            mImageView.setImageBitmap(bitmap);
            //storeImageTosdCard(bitmap);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    } else if (resultCode == RESULT_OK && requestCode == GALLERY_PICTURE) {
        if (data != null) {

            Uri selectedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor c = getActivity().getContentResolver().query(selectedImage, filePath,
                    null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            selectedImagePath = c.getString(columnIndex);
            c.close();

            bitmap = BitmapFactory.decodeFile(selectedImagePath); // load

            mImageView.setImageBitmap(bitmap);

        } else {
            Toast.makeText(getContext(), "Cancelled",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

这两种方法都适用于我,并显示缩略图

我尝试了两种方法,如从图库中选择图像和通过添加

android:requestLegacyExternalStorage="true"
我的代码与你的不同,但工作过程相同,但这对我来说是可行的

Uri imgUri=null;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
private Intent pictureActionIntent = null;
Bitmap bitmap;
String selectedImagePath;


private void SelectImage() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(
            getActivity());
    myAlertDialog.setTitle("Upload Pictures Option");
    myAlertDialog.setMessage("How do you want to set your picture?");

    myAlertDialog.setPositiveButton("Gallery",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent pictureActionIntent = null;

                    pictureActionIntent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(
                            pictureActionIntent,
                            GALLERY_PICTURE);

                }
            });

    myAlertDialog.setNegativeButton("Camera",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File file = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                    imgUri= FileProvider.getUriForFile(Objects.requireNonNull(getContext()), BuildConfig.APPLICATION_ID + ".provider", file);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);

                    startActivityForResult(intent, CAMERA_REQUEST);

                }
            });
    myAlertDialog.show();
}
OnActivityResult

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

    bitmap = null;
    selectedImagePath = null;

    if (resultCode == RESULT_OK && requestCode == CAMERA_REQUEST) {

        File f = new File(Environment.getExternalStorageDirectory()
                .toString());
        for (File temp : f.listFiles()) {
            if (temp.getName().equals("temp.jpg")) {
                f = temp;
                break;
            }
        }

        if (!f.exists()) {
            Toast.makeText(getContext(), "Error while capturing image", Toast.LENGTH_LONG).show();
            return;
        }

        try {
            bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
            bitmap = Bitmap.createScaledBitmap(bitmap, 400, 400, true);
            int rotate = 0;
            try {
                ExifInterface exif = new ExifInterface(f.getAbsolutePath());
                int orientation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);
                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotate = 270;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotate = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotate = 90;
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            Matrix matrix = new Matrix();
            matrix.postRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), matrix, true);

            mImageView.setImageBitmap(bitmap);
            //storeImageTosdCard(bitmap);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    } else if (resultCode == RESULT_OK && requestCode == GALLERY_PICTURE) {
        if (data != null) {

            Uri selectedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor c = getActivity().getContentResolver().query(selectedImage, filePath,
                    null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            selectedImagePath = c.getString(columnIndex);
            c.close();

            bitmap = BitmapFactory.decodeFile(selectedImagePath); // load

            mImageView.setImageBitmap(bitmap);

        } else {
            Toast.makeText(getContext(), "Cancelled",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

这两种方法都适用于我,显示的缩略图只更新了我的答案我遵循的:)它适用于Android 10和更低版本只更新了我遵循的答案:)它适用于Android 10和更低版本