从图库中选择图像并将其加载到特定活动的图像视图中,并加载到另一个活动的导航抽屉图像视图中 [英] Pick image from gallery and load it into the imageview of the particular activity and into a navigation drawer imageview in another activity

查看:122
本文介绍了从图库中选择图像并将其加载到特定活动的图像视图中,并加载到另一个活动的导航抽屉图像视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动个人资料,用户可以在其中更新他的个人资料图片。同时我有一个导航抽屉,其中更新的个人资料图片应该被加载到导航抽屉的图像视图中。我需要一个代码,用户可以从画廊或相机中选择图像并裁剪它以适应图像视图和应该将相同的图像加载到主活动中导航抽屉的图像视图中。

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.

这是我的ProfileAcitvity的代码在此代码中,我们可以从中选择图像gallery并加载到图像视图中。但是当我们尝试回到之前的活动并再次回到个人资料活动时,更新的图像就不存在了。我们要再次上传。

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();
            }
        }
    }
}

这个是导航抽屉图像
http://i.stack.imgur.com/JTLd3。 jpg

提前致谢

推荐答案

SharedPreference是最好的主意。从共享首选项中设置MainActivity中的图像。在从画廊或相机中选择图像后的ProfileActivity中将其保存到共享的首页。当您从ProfileActivity返回MainActivity时,还有一件事是在onBackPressed()中使用intent。

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().

例如

 @Override
    public void onBackPressed() {
        super.onBackPressed();

        Intent intent = new Intent(ProfileActivity.this, MainActivity.class);

        startActivity(intent);
    }

使用此步骤后,您的MainActivity中也会设置相同的图像。

After using this steps same image will be set in your MainActivity too.

这篇关于从图库中选择图像并将其加载到特定活动的图像视图中,并加载到另一个活动的导航抽屉图像视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆