问题
I have a Activity Profile in which the user can update his profile picture. At the same time I have a navigation drawer in which the updated profile picture should be loaded into the imageview of the navigation drawer.I need a code in which the user can choose image from gallery or camera and crop it to fit into the imageview and the same image should be loaded into the image view of a navigation drawer in the main activity.
Here is the code of my ProfileAcitvity In this code we can choose the image from gallery and is loaded into the image view. But when we try to go back to the previous activity and again come back to profile activity the updated image is no there. wehave to upload it again.
public class Profile extends AppCompatActivity {
ImageView picture;
private int PICK_IMAGE_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
picture = (ImageView)findViewById(R.id.picture);
picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(),"image clicked",Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
myPrefsEdit.putString("url", uri.toString());
myPrefsEdit.commit();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
picture.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
This is the navigation drawer image http://i.stack.imgur.com/JTLd3.jpg
Thanks in advance
回答1:
SharedPreference is the best idea. Set your image in MainActivity from sharedpreference. In ProfileActivity after selecting image from gallery or camera save it to shared pref. One more thing when you go back to MainActivity from ProfileActivity use intent in onBackPressed().
for e.g.
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(ProfileActivity.this, MainActivity.class);
startActivity(intent);
}
After using this steps same image will be set in your MainActivity too.
回答2:
in onCreate() do this :
Uri uriString = Uri.parse(myPrefsEdit.getString("url", ""));
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uriString);
// Log.d(TAG, String.valueOf(bitmap));
picture.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
回答3:
Once you load image from gallary than set image in oncreate method by uri which you save in mPrefEdit.
if(mPrefEdit.getString("url","").length>0){
picture.setImageURI(mPrefEdit.getString("url","");
}
来源:https://stackoverflow.com/questions/39785239/pick-image-from-gallery-and-load-it-into-the-imageview-of-the-particular-activit