简体   繁体   English

如何将从相机或画廊获得的图像传递到另一个活动

[英]How to pass image gotten from camera or gallery to another activity

1. User selects a button to either upload from gallery or capture from camera 1. 用户选择一个按钮从图库上传或从相机捕获

From gallery从画廊

    choose_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            // Sets the type as image/*. This ensures only components of type image are selected
            intent.setType("image/*");
            //We pass an extra array with the accepted mime types. This will ensure only components with these MIME types as targeted.
            String[] mimeTypes = {"image/jpeg", "image/png"};
            intent.putExtra(Intent.EXTRA_MIME_TYPES,mimeTypes);
            // Launching the Intent
            startActivityForResult(intent,1);
        }
    });

From camera从相机

capture_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(UploadActivity2.this, BuildConfig.APPLICATION_ID + ".provider", createImageFile()));
                startActivityForResult(intent, 0);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }
    });

2. User selects a photo from gallery or capture from camera and the image is displayed in the current activity 2. 用户从图库中选择一张照片或从相机拍摄,图像显示在当前活动中

public void onActivityResult(int requestCode,int resultCode,Intent data){
............//grant permission codes here

//If it is from gallery
if (requestCode == 1 && resultCode == RESULT_OK && null != data) { 
     Uri selectedImage = data.getData();
     String[] filePathColumn = { MediaStore.Images.Media.DATA };
     Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
     cursor.moveToFirst();   
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
     String imgDecodableString = cursor.getString(columnIndex);
     cursor.close();

     //Display image with glide
     Glide.with(this).asBitmap().load(imgDecodableString).into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? 
     super Bitmap> transition) {
                        display_image.setImageBitmap(resource);
                        display_image.setVisibility(View.VISIBLE);
                    }    
}

//If request is from camera
if (resultCode == Activity.RESULT_OK)
        switch (requestCode){
            case 0:
                //Display image in current activity 
                Glide.with(this)
                        .load(cameraFilePath)
                        .into(display_image);
                /*display_image.setImageURI(Uri.parse(cameraFilePath));*/
                display_image.setVisibility(View.VISIBLE);
                break;
        }

}

3. I have a 'NEXT' button and when clicked I want to transfer the image displayed (Gotten from either the Gallery or Camera) to another activity, I havn't written a code for passing the image yet 3.我有一个“NEXT”按钮,点击后我想将显示的图像(从图库或相机中获取)传输到另一个活动,我还没有编写传递图像的代码

next_upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(UploadActivity2.this, UploadActivity3.class);
            startActivity(intent);
        }
    });

4. I want to know the best way to do this without affecting image quality and memory because in the next activity (UploadAcitivity3), I will be uploading the image passed to the server and saving in a directory 4. 我想知道在不影响图像质量和内存的情况下执行此操作的最佳方法,因为在下一个活动 (UploadAcitivity3) 中,我将上传传递到服务器的图像并保存在目录中

Please follow the steps to achieve this:请按照以下步骤实现:

Option - 1: If you want to pass multiple images then use below:选项 - 1:如果要传递多个图像,请使用以下方法:

  • Step - 1: Store the selected images path in an ArrayList like below:步骤 - 1:将选定的图像路径存储在ArrayList如下所示:
private ArrayList<String> selectedImages = new ArrayList<>();

public void onActivityResult(int requestCode,int resultCode,Intent data) {

    ............//grant permission codes here

    //If it is from gallery
    if (requestCode == 1 && resultCode == RESULT_OK && null != data) {

        ....

        String imgDecodableString = cursor.getString(columnIndex);

        selectedImages.add(imgDecodableString);
    }

    //If request is from camera
    if (resultCode == Activity.RESULT_OK) {
        selectedImages.add(cameraFilePath);
    }
}
  • Step - 2: From onClick set the selected images list as extras to intent第 - 2 步:onClick将选定的图像列表设置为extrasintent
next_upload.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(UploadActivity2.this, UploadActivity3.class);
        intent.putStringArrayListExtra("SELECTED_IMAGES", selectedImages);
        startActivity(intent);
    }
});
  • Step - 3: Retrieve the selected images from intent in UploadActivity3 like below:步骤 - 3:UploadActivity3 intent中检索所选图像,如下所示:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {


    ....

    ArrayList<String> selectedImages = getIntent().getStringArrayListExtra("SELECTED_IMAGES");

}

Option - 2: If you want to pass single image then use below:选项 - 2:如果要传递单个图像,请使用以下方法:

  • Step - 1: Store the selected image path like below:步骤 - 1:存储选择的图像路径,如下所示:
private String selectedImage;

public void onActivityResult(int requestCode,int resultCode,Intent data) {

    ............//grant permission codes here

    //If it is from gallery
    if (requestCode == 1 && resultCode == RESULT_OK && null != data) {

        ....

        String imgDecodableString = cursor.getString(columnIndex);

        selectedImage = imgDecodableString;
    }

    //If request is from camera
    if (resultCode == Activity.RESULT_OK) {
        selectedImage = cameraFilePath;
    }
}
  • Step - 2: From onClick set the selected images list as extras to intent第 - 2 步:onClick将选定的图像列表设置为extrasintent
next_upload.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(UploadActivity2.this, UploadActivity3.class);
        intent.putExtra("SELECTED_IMAGE", selectedImage);
        startActivity(intent);
    }
});
  • Step - 3: Retrieve the selected images from intent in UploadActivity3 like below:步骤 - 3:UploadActivity3 intent中检索所选图像,如下所示:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {


    ....

    String selectedImage = getIntent().getStringExtra("SELECTED_IMAGE");
    Glide.with(this).load(selectedImage).into(image_view);
}

You can send the image path through Intent您可以通过Intent发送图像路径

next_upload.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(UploadActivity2.this, UploadActivity3.class);
        intent.putExtra("path", imagePath);
        startActivity(intent);
    }
});

You already have image path for capturing image is cameraFilePath您已经有用于捕获图像的图像路径是cameraFilePath

and for gallery image imgDecodableString .和画廊图像imgDecodableString

Declare String imagePath;声明String imagePath; as class variable and assign them in onActivityResult.作为类变量并在onActivityResult.分配它们onActivityResult.

imagePath = imgDecodableString;//For Gallery


imagePath = cameraFilePath;//For Capture image

Receive path in UploadActivity3.class UploadActivity3.class接收路径

String imagePath = getIntent().getStringExtra("path");

Use this path in second activity as you want.根据需要在第二个活动中使用此路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM