Android - 将每个连接或捕获的图像调整为768px宽度

时间:2017-07-14 11:23:51

标签: android bitmap autoresize

这是我的代码

@OnClick(R.id.choose_from_gallery_button)
public void pickImageFromGallery() {
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
                PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
    } else {
        sendOpenGalleryIntent();
    }
}

private void sendOpenGalleryIntent(){
    floatingMenu.close(true);
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    startActivityForResult(intent, CHOOSE_FROM_GALERY_REQUEST_CODE);
}


@OnClick(R.id.capture_photo_button)
public void capturePhoto() {
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
                PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
    } else {
        sendStartCameraIntent();
    }
}

private void sendStartCameraIntent(){
    floatingMenu.close(true);
    try {
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File tmpImageFile = createImageFile();

        //Uri used between Camera and MyMusala apps, created with FileProvider to support Android Nougat FileProvider flow
        Uri cameraUri = FileProvider.getUriForFile(this,
                BuildConfig.APPLICATION_ID + ".provider",tmpImageFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);

        //Internal uri use only
        attachmentUri = Uri.fromFile(tmpImageFile);

        startActivityForResult(intent, TAKE_PICTURE_REQUEST_CODE);
    } catch (IOException e) {
        Toast.makeText(this, R.string.could_not_start_camera , Toast.LENGTH_SHORT).show();
        FirebaseCrash.report(e);
        Log.e(TAG, "Could not create tmp file for taking picture", e);
    }
}

我希望能够将附件的自动调整大小设置为768像素宽度(当按下提交按钮时会将其发送到邮件,因此我不想加载带存储的服务器。)

有人可以帮帮我吗?

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK && data != null) {
        if (requestCode == CHOOSE_FROM_GALERY_REQUEST_CODE) {
            attachmentUri = data.getData();
        }
        Picasso.with(this).load(attachmentUri).into(imagePreview);
        attachmentImageName.setText(UriHelper.getFileName(this,attachmentUri));
        changeAttachmentVisibility(true);

    } else if (resultCode != Activity.RESULT_OK && requestCode == TAKE_PICTURE_REQUEST_CODE){
        //If take picture failed does not send attachment
        try {
            new File(attachmentUri.getPath()).delete();
        }catch (Exception e){
            //do nothing
        }
        attachmentUri = null;
    }
}

0 个答案:

没有答案