希望在cardview中动态上载图像

lokaqttq  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(366)

所以基本上我在下面附上了addimage.java代码和mainpage.xml代码。每当用户单击addimage按钮时,他都可以选择image并在mymainpage.xml文件中动态添加图像。所以基本上就像我想通过点击按钮将图像从一个活动转移到另一个活动。我试过这样做,但我还是坚持了一个特定的点,用户可以选择从画廊的图像,但我仍然不知道如何将它上传到mainpage.xml文件。
任何帮助都是值得赞赏的。
这是我的addimage.java代码

public class addimage extends AppCompatActivity {

    private ImageView imageView;
    private Button addimage;
    private ProgressBar progressBar;

    android.content.Intent data;

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

        imageView = (ImageView) findViewById(R.id.imageView);
        addimage = (Button) findViewById(R.id.addimage);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        progressBar.setVisibility(View.INVISIBLE);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent();
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent, 1);
            }
        });

        addimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageView != null) {
                    /*uploadToFirebase(imageView);*/
                } else {
                    Toast.makeText(getApplicationContext(), "PLEASE SELECT IMAGE", Toast.LENGTH_SHORT).show();
                }

            }
        });

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           /* super.onActivityResult(requestCode, resultCode, data);*/
            switch (requestCode) {
                case 2://2 is from startActivityForResult(galleryIntent, 2);
                    try {
                        Uri selectedImage = data.getData(); //get photo uri
                        imageView.setImageURI(selectedImage);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }

    }
}

这是我的mainpage.xml代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/mainpage"
    android:layout_height="match_parent"
    tools:context=".mainpage">

    <LinearLayout
        android:orientation="vertical"
        android:background="@color/Pink"
        android:weightSum="10"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2">

            <TextView
                android:layout_width="950dp"
                android:layout_height="550dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="5dp"
                android:layout_marginBottom="0dp"
                android:alpha="0.5"
                android:background="@color/purple_200"
                android:text="     BEGINNERS CALLIGRAPHY"
                android:textAllCaps="false"
                android:textColor="@color/DarkBlue"
                android:textSize="25sp"
                android:textStyle="bold" />

            <Button
                android:layout_width="110dp"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="210dp"
                android:layout_marginBottom="7dp"
                android:text="PHOTOS"
                android:textSize="20dp"
                android:background="@color/white"
                android:textColor="@color/purple_700"/>

            <Button
                android:id="@+id/button1"
                android:layout_width="110dp"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="52dp"
                android:layout_marginBottom="7dp"
                android:text="VIDEOS"
                android:textSize="20dp"
                android:background="@color/white"
                android:textColor="@color/purple_700"/>

        </RelativeLayout>

        <GridLayout
            android:id="@+id/mainGrid"
            android:layout_width="match_parent"
            android:layout_height="949dp"
            android:layout_weight="8"
            android:alignmentMode="alignMargins"
            android:columnCount="2"
            android:columnOrderPreserved="false"
            android:padding="14dp"
            android:rowCount="3">

            <androidx.cardview.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp"
                android:background="@color/Purple">

                <LinearLayout
                    android:layout_width="149dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageview_image"
                        android:layout_width="150dp"
                        android:layout_height="180dp"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/b1" />

                </LinearLayout>

            </androidx.cardview.widget.CardView/>

            <!-- Row 2 -->

            <!-- Column 1 -->
            <androidx.cardview.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="149dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageview_image2"
                        android:layout_width="150dp"
                        android:layout_height="180dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/b3" />

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <!-- Column 2 -->
            <androidx.cardview.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="149dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageview_image3"
                        android:layout_width="190dp"
                        android:layout_height="180dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/b4" />

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <!-- Row 2 -->

            <!-- Column 1 -->
            <androidx.cardview.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="149dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageview_image4"
                        android:layout_width="150dp"
                        android:layout_height="200dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/b5" />

                </LinearLayout>

            </androidx.cardview.widget.CardView>

        </GridLayout>

        <ImageView
            android:id="@+id/expanded_image"
            android:layout_marginRight="30dp"
            android:layout_width="350dp"
            android:layout_height="900dp"
            android:visibility="invisible"
            android:layout_gravity="center"/>
        <!--android:layout_gravity="center"-->

    </LinearLayout>

</ScrollView>

如果需要更多的代码,请在下面评论。

cuxqih21

cuxqih211#

尝试将代码添加到此addimage类:

public class addimage extends AppCompatActivity {

private ImageView imageView;
private Button addimage;
private ProgressBar progressBar;
android.content.Intent data;

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

    imageView = (ImageView) findViewById(R.id.imageView);
    addimage = (Button) findViewById(R.id.addimage);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setVisibility(View.INVISIBLE);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent();
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent, 1);
        }
    });

    addimage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (imageView != null) {
                /*uploadToFirebase(imageView);*/
            } else {
                Toast.makeText(getApplicationContext(), "PLEASE SELECT IMAGE", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 1://1 is from startActivityForResult(galleryIntent, 2);
            try {
                Uri selectedImage = data.getData(); //get photo uri
                imageView.setImageURI(selectedImage);
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
    }
}
}

相关问题