so if the title sounds confusig, this is what actually happens. In the Activity I have a ImageView and it has a defalut picture. This defalut picture should be shown as long as the user picks another picture from gallery. When he does that, the picture which he picked should be shown as long as he doesn't delete it or picks another one. If he deletes it, the default picture should be shown again until he picks new picture from the gallery. I have successfuly loaded the picture from the gallery but it stays in the imageview only until the applications restarts. After which the default picture will be shown again. Is there any way to fix this?
This is my code
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));
}
}
}
layout
ImageView has id imgUser, located in 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" />
Thx for help!
The default image is 'shown again' every time the view are (re)built i.e. every time the Activity
is created, when the application is started, but also on device rotation. You should save the path to the selected image (in onActivityResult()
) and retrieve it in onCreate()
. The simplest way to save the path is probably to use a SharedPreferences.
If you want to restrict the selection to local images, add the EXTRA_LOCAL_ONLY extra to the intent.