简体   繁体   English

如何从图库中选择图像并在其他活动中显示?

[英]How can i select an image from gallery and display that in another activity..?

i have created the code to select image from gallery,but i can not pass that value to another activity through bundle..please help me 我已经创建了从图库中选择图像的代码,但是我无法通过捆绑软件将该值传递给另一个活动。请帮助我

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            img.setImageURI(selectedImageUri);


enter code here

i need to pass the SelectedImageUri to another activity as bundle 我需要将SelectedImageUri作为捆绑包传递给另一个活动

Use this 用这个

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
                Intent intent = new Intent(this , Second_activity.class );
                intent.putExtra("image_path", selectedImagePath);
                startActivity(intent);
}

it will start the Second Activity, then on the second Activity receive those values by this 它将启动第二个Activity,然后在第二个Activity上接收这些值

 Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("image_path");
        //use value
    }

I know is not exactly what you were looking for but if you pass selectedImagePath using i.putExtra("photoPath", selectedImagePath); 我知道不是您要找的东西,但是如果您使用i.putExtra("photoPath", selectedImagePath);传递selectedImagePath i.putExtra("photoPath", selectedImagePath); , you can later load the image using only the path. ,以后可以仅使用路径加载图像。

I need to pass the SelectedImageUri to another activity as bundle 我需要将SelectedImageUri作为捆绑包传递给另一个活动

=> FYI, Uri class itself implement Parcelable, so you can add and get value into/from Intent directly. =>仅供参考,Uri类本身实现了Parcelable,因此您可以直接向Intent添加值或从Intent获取值。

// Add a Uri instance to an Intent
intent.putExtra("SelectedImageUri", SelectedImageUri);

// Get a Uri from an Intent
Uri uri = intent.getParcelableExtra("SelectedImageUri");

暂无
暂无

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

相关问题 从图库中选择图像,然后在另一个活动中显示 - Select a image from the gallery and show it in another Activity 如何将图片库从片段传递到另一个活动? - How to passing Image Gallery from fragment into another activity? 如何 select 从设备的图库中获取图像并将其显示在图像视图上 - How to select an image from the device's gallery and display it on an image view 如何将从相机或画廊获得的图像传递到另一个活动 - How to pass image gotten from camera or gallery to another activity Select 来自图库的照片并在另一个活动中显示 - Select a photo from the gallery and show it in another Activity 从图库中选择一个图像并发送到另一个活动 - Choose a Image from Gallery and send to another Activity 如何使用 android 代码从图库中 select 多个图像并在单个活动中显示它们? - How to select multiple images from gallery and display them in a single activity using android code? 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio 我将如何将最新图像显示到 ImageView 另一个活动? - How I will display latest image to ImageView Another Activity? 从android图像库中选择并将其加载到另一个活动中不起作用 - picking from android image gallery and loading it into another activity not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM