我要允许用户从用户设备在imageView中添加图像吗?

时间:2019-02-02 13:17:41

标签: android

我正在将布局动态添加到另一个包含imageView的布局中。我正在尝试将点击列表添加到该imageView中,以便用户可以从其设备将图像添加到该imageview中,但是它不起作用

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:layout_height="match_parent"
    tools:context="com.example.myapplication.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#dddd"
            android:id="@+id/parent_linear_layout">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/addlayouts"
                android:text="addlayouts"/>

        </LinearLayout>
    </ScrollView>

</LinearLayout>


activity_asset2.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp">

        <ImageView
            android:id="@+id/img1"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:background="@android:color/holo_blue_light"
            android:onClick="selectImage"
            android:layout_margin="2dp"
            />
        <EditText
            android:id="@+id/textArea_information"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:gravity="top|left"
            android:background="#ffffff"
            android:inputType="textMultiLine"
            android:overScrollMode="always"
            android:scrollbarStyle="insideInset"
            android:padding="5dp"
            android:hint="Enter your text here"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:scrollHorizontally="false" />
    </LinearLayout>

</LinearLayout>


MainActivity.java


    Button bodybtn, btn2;
    ArrayList<View> views = new ArrayList<View>();
    public LinearLayout parentLinearLayout,parent_linear_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
        bodybtn = (Button) findViewById(R.id.addlayouts);
        bodybtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popup();
            }
        });
    }
        public void popup(){
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Asset Store");
            builder.setItems(new CharSequence[]
                            {"left image with text","2 images"},
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // The 'which' argument contains the index position
                            // of the selected item
                            switch (which) {
                                case 0:
                                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                    final View rowView = inflater.inflate(R.layout.activity_asset2, null);
                                    // Add the new row before the add field button.
                                    parentLinearLayout.addView(rowView,parentLinearLayout.getChildCount() - 1);
                                    views.add(rowView);
                                    break;
                                case 1:
                                    LayoutInflater inflater1 = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                    final View rowView1 = inflater1.inflate(R.layout.activity_asset3, null);
                                    // Add the new row before the add field button.
                                    parentLinearLayout.addView(rowView1, parentLinearLayout.getChildCount() - 1);
                                    views.add(rowView1);
                                    break;
                            }
                        }
                    });
            builder.create().show();


    }

}

asset2.java

    public static final int SELECT_PICTURE = 0;
    public ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asset2);
        imageView = (ImageView) findViewById(R.id.img1);

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bitmap bitmap = getPath(data.getData());
            imageView.setImageBitmap(bitmap);
        }
    }

    private Bitmap getPath(Uri uri) {

        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String filePath = cursor.getString(column_index);
        // cursor.close();
        // Convert file path into bitmap image using below line.
        Bitmap bitmap = BitmapFactory.decodeFile(filePath);

        return bitmap;
    }

    private void selectImage() {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
    }

}

我希望当用户单击imageview时,他应该去设备图库并选择图像,然后将该图像显示在该imageview上。 但是当我单击imageview时,我的应用程序关闭了。我不知道出了什么问题。

1 个答案:

答案 0 :(得分:0)

由于我无法写评论,因此请查看this帖子,在本教程的底部,有一些类用于管理加载图像。