因此,在主要活动中,我将图像视图更改为来自图库的图片。我想将另一个活动中的ImageButton图像更改为所选的图片。
答案 0 :(得分:1)
MainActivity:
private static int RESULT_LOAD_IMAGE = 1;
private final String FILEPATH = "FilePath";
SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
}
});
}
@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();
prefs.edit().putString(FILEPATH, picturePath);
}
}
然后使用SharedPreferences获取imagePath并在anotherActivity()
anotherActivity()
:
public class anotherActivity extends MainActivity{
private final String FILEPATH = "FinalPath";
String path;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstance);
setContentView(R.layout.main);
path = prefs.getString(FILEPATH, "");
ImageButton imageButton = (ImageView) findViewById(R.id.imgBtn);
imageButton.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeFile(path)));
}
}