从图库中挑选图像并将其加载到特定活动的图像视图中,并加载到另一个活动的导航抽屉imageview中
我有一个活动配置文件,用户可以在其中更新其个人资料图片。同时,我有一个导航抽屉,其中更新后的个人资料图片应该加载到导航抽屉的图像视图中。我需要一个代码,用户可以从代码库或照相机中选择图像并剪裁它以适应图像视图,相同的图像应该加载到主要活动中导航抽屉的图像视图中。从图库中挑选图像并将其加载到特定活动的图像视图中,并加载到另一个活动的导航抽屉imageview中
这里是我的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
在此先感谢
SharedPreference是最好的主意。在MainActivity中设置您的图像来自sharedpreference。在从图库或照相机中选择图像后,在ProfileActivity中将其保存到共享前缀。当您从ProfileActivity返回MainActivity时,还有一件事在onBackPressed()中使用了intent。
例如,
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(ProfileActivity.this, MainActivity.class);
startActivity(intent);
}
使用此步骤后,您的MainActivity中也会设置相同的图像。
在onCreate()
做到这一点:一旦你从GALLARY加载图像比在onCreate方法集图像
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();
}
回到主要活动后,图像仅在配置文件活动图像视图中更新。我们上传新图片时如何更新图片视图。 – vishnumm93
由uri保存在mPrefEdit中。
if(mPrefEdit.getString("url","").length>0){
picture.setImageURI(mPrefEdit.getString("url","");
}
返回主要活动后,图像仅在配置文件活动图像视图中更新。我们上传新图片时如何更新图片视图。 – vishnumm93
在主要活动中使用相同的prefrence图像路径保存在mPrefEdit中获取路径并在您想要的任何图像视图上设置图像 –
图像仅在回到主要活动后在配置文件活动图像视图中更新。我们上传新图片时如何更新图片视图。 – vishnumm93
我知道我已经阅读过你的问题。阅读我的回答 –
你上传图像到服务器? –