点击按钮从图库中添加图片这是我写的用来接收图片的布局:
<
Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="选择图片"/>
<
ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</LinearLayout>
</ScrollView>
下面是添加图片的代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
System.out.println("error");
return;
}
if (requestCode == IMAGE_CODE) {
Uri uri = data.getData();
//先把图片加入界面中
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
ImageView iv=new ImageView(MainActivity.this);
iv.setImageBitmap(bitmap);
lv.addView(iv, i);
i=i++;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
点击添加选择完成后确实能添加,但是我在添加第二张的时候,第一张却没了,只有第二张?求教,怎么才能留住第一张