【发布时间】:2016-09-30 17:31:36
【问题描述】:
我有一个活动资料,用户可以在其中更新他的资料图片。同时我有一个导航抽屉,其中更新的个人资料图片应该加载到导航抽屉的图像视图中。我需要一个代码,用户可以在其中从画廊或相机中选择图像并将其裁剪以适合图像视图和应将相同的图像加载到主活动中导航抽屉的图像视图中。
这是我的 ProfileAcitvity 的代码 在此代码中,我们可以从图库中选择图像并加载到图像视图中。但是当我们尝试返回上一个活动并再次返回配置文件活动时,更新的图像不存在。我们必须重新上传。
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();
}
}
}
}
这是导航抽屉图像 http://i.stack.imgur.com/JTLd3.jpg
提前致谢
【问题讨论】: