Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android动态图像视图_Android_Imageview_Android Linearlayout - Fatal编程技术网

Android动态图像视图

Android动态图像视图,android,imageview,android-linearlayout,Android,Imageview,Android Linearlayout,我正在制作一个自定义布局,我想在其中从库中动态添加图像视图。但是,当我从画廊中挑选图像时,我就迫不及待地靠近了。我无法得到同样的错误背后是什么 这是我的密码 public class Dynamic extends Activity { int i; ImageView[] img_items; ArrayList<String> values = new ArrayList<String>(); LinearLayout imageLayout; Button btn;

我正在制作一个自定义布局,我想在其中从库中动态添加图像视图。但是,当我从画廊中挑选图像时,我就迫不及待地靠近了。我无法得到同样的错误背后是什么

这是我的密码

public class Dynamic extends Activity {
int i;
ImageView[] img_items;
ArrayList<String> values = new ArrayList<String>();
LinearLayout imageLayout;
Button btn;
private static int RESULT_LOAD_IMAGE = 1;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dynamic_load);

    imageLayout = (LinearLayout)findViewById(R.id.image_layout);
    btn = (Button)findViewById(R.id.btn_ADD);
    img_items = new ImageView[values.size()];

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            values.add("AA");

            Intent i = new Intent(
                    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);

        }
    });


}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && 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 picturePath = cursor.getString(columnIndex);
        cursor.close();

        /*ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));*/


        //Creating the image layouts dynamically
        for( i=0;i<values.size();i++){

            img_items[i] = new ImageView(Dynamic.this);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

            params.setMargins(10, 10, 0, 0);
            img_items[i].setLayoutParams(params);

        }

        img_items[i].setImageBitmap(BitmapFactory.decodeFile(picturePath));
        imageLayout.addView(img_items[i]);


    }
}
}

您需要将
i
值减少1,因为
i
在for循环中是递增的,例如,您迭代了
5
时间,因此for循环将是迭代
5
时间,从
0
开始,以
4
结束,但此时
i
值先递增,然后将递增检查2表达式当时
i
值和
值的长度和for
i
是否相同。size()
可以终止for循环以执行其余代码

已编辑

if(i==values.size() && i>0)
   i--;

img_items[i].setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageLayout.addView(img_items[i]);

还有一件事你需要设置
i=0
,每次你在
onActivityResult()中
另一方面,你总是得到
i
last value

你需要将
i
值减少一,因为
i
在for循环中是递增的。例如,你迭代了
5
时间,因此for循环将迭代
5
时间,从
0
开始,以
4
结束,但在此时,首先递增
i
值,然后它将检查2表达式,此时
i
值和
值的长度和for是否相同。size()
因此它将终止for循环以执行其余代码

已编辑

if(i==values.size() && i>0)
   i--;

img_items[i].setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageLayout.addView(img_items[i]);

还有一件事你需要设置
i=0
每次当你在
onActivityResult()
中时,你总是会得到
i
最后一个值

你在onCreate中初始化数组img\u项:

img_items = new ImageView[values.size()];
此时values.size()为“零”

您应该在onActivityResult中的for循环之前执行此初始化:

    img_items = new ImageView[values.size()];
    for( i=0;i<values.size();i++){
    ...
img_items=newimageview[values.size()];

对于(i=0;i在onCreate中初始化数组img_项:

img_items = new ImageView[values.size()];
此时values.size()为“零”

您应该在onActivityResult中的for循环之前执行此初始化:

    img_items = new ImageView[values.size()];
    for( i=0;i<values.size();i++){
    ...
img_items=newimageview[values.size()];

对于(i=0;i在看到代码后,当您使用value.size()初始化ImageView数组时,我可以这么说

此时,“值”数组列表的大小为0,因此ImageView数组的大小也为0,然后将ImageView放入onActivityResult()中大小为0的数组中


看过代码后,我可以这样说,当您使用value.size()初始化ImageView数组时

此时,“值”数组列表的大小为0,因此ImageView数组的大小也为0,然后将ImageView放入onActivityResult()中大小为0的数组中

试试这个

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</LinearLayout>
试试这个

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</LinearLayout>

下面是从ImageIntent加载多个图像的代码

uriselectedimage=data.getData();
字符串[]filePathColumn={MediaStore.Images.Media.DATA};
Cursor Cursor=getContentResolver().query(
选择图像、文件路径列、null、null、null);
cursor.moveToFirst();

对于(inti=0;i,下面是从image intent加载多个图像的代码

uriselectedimage=data.getData();
字符串[]filePathColumn={MediaStore.Images.Media.DATA};
Cursor Cursor=getContentResolver().query(
选择图像、文件路径列、null、null、null);
cursor.moveToFirst();

对于(int i=0;我总是添加logcat错误报告。始终添加logcat错误报告。Pratik谢谢,但你的英语不太容易理解。请不要难过。我这样说是为了改进,因为bro.for循环首先执行主体,然后增加索引值。因此,对我来说,上述工作流程不正确。如果你这样做,我--;那么值将在第一次执行时更改为-1,数组索引不能为-1。是的,另一个答案也是正确的。您需要使用其大小初始化img_项。存在名称问题。没有更改,谢谢,但您的英语不太容易理解。请不要难过。我这样说是为了改进,因为bro.for循环首先执行主体,然后递增输入索引的值。因此,对我来说,上述工作流程不正确。如果您这样做,则在第一次执行时,值将更改为-1,数组索引不能为-1。是的,另一个答案也正确。您需要使用其大小初始化img_项。存在名称问题。不存在更改相同的问题。不存在更改相同的问题。不存在c为什么你要给我备选方案你的方案完全不同,你只是为一个图像视图这样做。我只是想动态添加到任何数量的图像视图。为什么你要给我备选方案。你的方案完全不同,你只是为一个图像视图这样做。我只是想动态添加到任何数量的图像视图。
  Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(
                               selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            for(int i=0;i<cursor.getCount();i++)
            {
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);

            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
            ImageView img_items = new ImageView(AnimatedPopupActivity.this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            params.setMargins(10, 10, 0, 0);
            img_items.setLayoutParams(params);

            img_items.setImageBitmap(yourSelectedImage);
            imageLayout.addView(img_items);



            cursor.moveToNext();
            }
            cursor.close();