【问题标题】:Pick image from gallery and load it into the imageview of the particular activity and into a navigation drawer imageview in another activity从图库中选择图像并将其加载到特定活动的图像视图中,并加载到另一个活动中的导航抽屉图像视图中
【发布时间】: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

提前致谢

【问题讨论】:

    标签: android imageview


    【解决方案1】:

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

    例如

     @Override
        public void onBackPressed() {
            super.onBackPressed();
    
            Intent intent = new Intent(ProfileActivity.this, MainActivity.class);
    
            startActivity(intent);
        }
    

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

    【讨论】:

    • 返回 mainactivity 后,图像仅在配置文件活动图像视图中更新。上传新图片时如何现场更新图片视图。
    • 你要上传图片到服务器吗?
    • 不,我没有上传到服务器只是为了我布局中的图像视图
    • 那么它非常简单,如果共享首选项中存在值,则显示图像,否则显示默认图像。阅读我的答案,您将获得解决方案
    • 非常感谢您
    【解决方案2】:

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

    【讨论】:

    • 返回 mainactivity 后,图像仅在配置文件活动图像视图中更新。上传新图片时如何现场更新图片视图。
    【解决方案3】:

    一旦您从画廊加载图像而不是通过保存在 mPrefEdit 中的 uri 在 oncreate 方法中设置图像。

    if(mPrefEdit.getString("url","").length>0){
        picture.setImageURI(mPrefEdit.getString("url","");
    }
    

    【讨论】:

    • 返回 mainactivity 后,图像仅在配置文件活动图像视图中更新。上传新图片时如何现场更新图片视图。
    • 在 mainactivity 中使用相同的优先级图像路径保存在 mPrefEdit 中获取路径并在您想要的任何图像视图上设置图像
    猜你喜欢
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    相关资源
    最近更新 更多