【发布时间】:2014-01-26 20:00:52
【问题描述】:
所以如果标题听起来很混乱,这就是实际发生的情况。在活动中,我有一个 ImageView,它有一张默认图片。只要用户从图库中选择另一张图片,就应该显示此默认图片。当他这样做时,只要他不删除它或选择另一张,就应该显示他选择的图片。如果他删除它,则应再次显示默认图片,直到他从图库中选择新图片。 我已经成功地从图库中加载了图片,但它只保留在 imageview 中,直到应用程序重新启动。之后将再次显示默认图片。有没有办法解决这个问题?
这是我的代码
package com.pumperlgsund.activities;
imports...
public class MainActivity extends FragmentActivity implements
OnHeadlineSelectedListener, View.OnClickListener {
private static int LOAD_IMAGE = 1;
private String selectedImagePath;
private ImageView imgUser;
private TextView txtName;
private TextView txtScore;
private TextView txtValue;
private UserDataController controller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/ComicNeueSansID.ttf");
controller = new UserDataController(this);
imgUser = (ImageView) findViewById(R.id.imgUser);
imgUser.setOnClickListener(this);
txtName = (TextView) findViewById(R.id.txtUserName);
txtName.setTypeface(tf);
txtName.setText(controller.getUserName());
txtScore = (TextView) findViewById(R.id.txtScore);
txtScore.setTypeface(tf);
txtValue = (TextView) findViewById(R.id.txtValue);
txtValue.setTypeface(tf);
txtValue.setText("" + controller.getScore());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imgUser:
onImageClicked(v);
break;
default:
break;
}
}
private void onImageClicked(View view) {
showPopupMenu(view);
}
private void showPopupMenu(View view) {
PopupMenu popupMenu = new PopupMenu(this, view);
popupMenu.getMenuInflater().inflate(R.menu.popupmenu,
popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.item1) {
//TODO: ...
return true;
} else if (item.getItemId() == R.id.item2) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Load Image"), LOAD_IMAGE);
return true;
} else {
return false;
}
}
});
popupMenu.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == LOAD_IMAGE) {
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();
imgUser.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
布局
ImageView 有 id imgUser,位于 RelativLayout user_area_content
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@color/backgroundColor"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<LinearLayout
android:id="@+id/static_area"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/user_area"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@color/white" >
<RelativeLayout
android:id="@+id/user_area_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageView
android:id="@+id/imgUser"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="8dip"
android:layout_marginTop="8dip"
android:src="@drawable/ic_img_user" />
<TextView
android:id="@+id/txtUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginTop="8dip"
android:layout_toRightOf="@id/imgUser"
android:text="@string/placeholder"
android:textColor="@color/blue"
android:textSize="16sp" />
<TextView
android:id="@+id/txtScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txtUserName"
android:layout_marginLeft="8dip"
android:layout_marginTop="8dip"
android:layout_toRightOf="@id/imgUser"
android:text="@string/score"
android:textColor="@color/blue"
android:textSize="16sp" />
<TextView
android:id="@+id/txtValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txtUserName"
android:layout_marginLeft="8dip"
android:layout_marginTop="8dip"
android:layout_toRightOf="@id/txtScore"
android:text="@string/placeholder"
android:textColor="@color/blue"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
<fragment
android:id="@+id/headlines_fragment"
android:name="com.pumperlgsund.fragments.HeadlinesFragment"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_marginTop="16dip"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="@+id/dynamic_frame"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="16dip"
android:layout_weight="2"
android:orientation="horizontal" />
谢谢帮助!
【问题讨论】:
-
你把所选图片的URI保存在哪里?
-
还没有这样做...你的意思是我应该保存 uri 并将其传递给 oncreate 方法中的 imageview。我想,当用户删除所选图片时,uri 将为空,在这种情况下,我应该显示默认图片,对吧?你说的uri是什么意思?图片路径还是?
标签: android android-imageview android-gallery