Android 安卓:拍摄照片,存储在SD卡上,然后覆盖它以创建更小的照片

Android 安卓:拍摄照片,存储在SD卡上,然后覆盖它以创建更小的照片,android,android-image,fileinputstream,bitmapfactory,Android,Android Image,Fileinputstream,Bitmapfactory,以下是我重现此场景的代码: 用户点击“拍照”按钮并拍照 用户单击“创建一次”按钮,将捕获的照片上载到服务器 所有这些部分都工作正常,但我想解决的问题是上传图像的大小太大:大约2Mo。 我想创建一个捕获图像的阴影副本,以便在第一张照片的基础上创建一张更小的照片。(如果可能,我们可以覆盖第一个) 我不知道该怎么做,在我的代码中该在哪里做 初始化目标文件的代码: String uniqueID = UUID.randomUUID().toString(); String name

以下是我重现此场景的代码:

  • 用户点击“拍照”按钮并拍照
  • 用户单击“创建一次”按钮,将捕获的照片上载到服务器
所有这些部分都工作正常,但我想解决的问题是上传图像的大小太大:大约2Mo。

我想创建一个捕获图像的阴影副本,以便在第一张照片的基础上创建一张更小的照片。(如果可能,我们可以覆盖第一个)

我不知道该怎么做,在我的代码中该在哪里做

初始化目标文件的代码:

String uniqueID = UUID.randomUUID().toString();
        String name = dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");
        imageToStore = name + uniqueID + ".jpg";
        destination = new File(Environment.getExternalStorageDirectory(), imageToStore);
takePhoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
                startActivityForResult(intent, REQUEST_IMAGE);

                overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
            }
        });
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
            try {
                preview.setVisibility(View.VISIBLE);
                takePhoto.setVisibility(View.GONE);
                txtHaut.setText("Cette image est parfaite !");
                FileInputStream in = new FileInputStream(destination);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                imagePath = destination.getAbsolutePath();
                Log.d("INFO", "PATH === " +imagePath);
                Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
                preview.setImageBitmap(bmp);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
        else{
            tvPath.setText("Request cancelled");
        }
    }
btnCreate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(New_annonce_act_step3.this, "", "Création de votre annonce en cours ..", true);

                new Thread(new Runnable() {
                    public void run() {       
                        uploadFile(imagePath);
                    }
                }).start();

            }

        });
当用户单击按钮时,它将启动照片捕获:

String uniqueID = UUID.randomUUID().toString();
        String name = dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");
        imageToStore = name + uniqueID + ".jpg";
        destination = new File(Environment.getExternalStorageDirectory(), imageToStore);
takePhoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
                startActivityForResult(intent, REQUEST_IMAGE);

                overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
            }
        });
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
            try {
                preview.setVisibility(View.VISIBLE);
                takePhoto.setVisibility(View.GONE);
                txtHaut.setText("Cette image est parfaite !");
                FileInputStream in = new FileInputStream(destination);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                imagePath = destination.getAbsolutePath();
                Log.d("INFO", "PATH === " +imagePath);
                Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
                preview.setImageBitmap(bmp);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
        else{
            tvPath.setText("Request cancelled");
        }
    }
btnCreate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(New_annonce_act_step3.this, "", "Création de votre annonce en cours ..", true);

                new Thread(new Runnable() {
                    public void run() {       
                        uploadFile(imagePath);
                    }
                }).start();

            }

        });
处理捕获的代码:

String uniqueID = UUID.randomUUID().toString();
        String name = dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");
        imageToStore = name + uniqueID + ".jpg";
        destination = new File(Environment.getExternalStorageDirectory(), imageToStore);
takePhoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
                startActivityForResult(intent, REQUEST_IMAGE);

                overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
            }
        });
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
            try {
                preview.setVisibility(View.VISIBLE);
                takePhoto.setVisibility(View.GONE);
                txtHaut.setText("Cette image est parfaite !");
                FileInputStream in = new FileInputStream(destination);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                imagePath = destination.getAbsolutePath();
                Log.d("INFO", "PATH === " +imagePath);
                Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
                preview.setImageBitmap(bmp);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
        else{
            tvPath.setText("Request cancelled");
        }
    }
btnCreate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(New_annonce_act_step3.this, "", "Création de votre annonce en cours ..", true);

                new Thread(new Runnable() {
                    public void run() {       
                        uploadFile(imagePath);
                    }
                }).start();

            }

        });
最后,当用户单击“创建一次”按钮时,它上传照片:

String uniqueID = UUID.randomUUID().toString();
        String name = dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");
        imageToStore = name + uniqueID + ".jpg";
        destination = new File(Environment.getExternalStorageDirectory(), imageToStore);
takePhoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
                startActivityForResult(intent, REQUEST_IMAGE);

                overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
            }
        });
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
            try {
                preview.setVisibility(View.VISIBLE);
                takePhoto.setVisibility(View.GONE);
                txtHaut.setText("Cette image est parfaite !");
                FileInputStream in = new FileInputStream(destination);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                imagePath = destination.getAbsolutePath();
                Log.d("INFO", "PATH === " +imagePath);
                Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
                preview.setImageBitmap(bmp);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
        else{
            tvPath.setText("Request cancelled");
        }
    }
btnCreate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(New_annonce_act_step3.this, "", "Création de votre annonce en cours ..", true);

                new Thread(new Runnable() {
                    public void run() {       
                        uploadFile(imagePath);
                    }
                }).start();

            }

        });

为什么不上传为预览图像视图创建的新位图?检查@PreetiPatwa如果我完全理解您引用的帖子中的代码,decodeFile会返回要上传的新图像的地址吗?事实上,第一个已经被覆盖了?不,没有被覆盖。它正在创建一个新位图,将其保存到临时文件并上载该临时文件。我不知道如何作为DESIREWIDTH和DESIREHEIGHT的参数传递:
imagePath=decodeFile(imagePath,bmp.getWidth(),bmp.getHeight())?。所需宽度*所需高度*4==2mb。计算当前的宽高比,并相应地设置desiredWidth和desiredHeight。