在android中更新后存储用户profle图片

时间:2016-03-05 04:23:52

标签: android

我想获取用户图像并存储图像,以便当用户再次打开应用程序时,它会找到其用户个人资料图片。我可以选择图像,但在销毁应用程序并再次打开后无法再次查看

//this how i pick the image
final ImageView image = (ImageView)findViewById(R.id.btn_search);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popup = new PopupMenu(Detailed.this, image);
                popup.getMenuInflater().inflate(R.menu.menu_detailed, popup.getMenu());
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        int id = item.getItemId();

                        //noinspection SimplifiableIfStatement
                        if (id == R.id.action_settings) {
                           // Toast.makeText(Detailed.this, "You Clicked : " + item.getItemId(), Toast.LENGTH_SHORT).show();
                            Intent i = new Intent(
                                    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                            startActivityForResult(i, RESULT_LOAD_IMAGE);
                        }
                        //Toast.makeText(Detailed.this, "You Clicked : " + item.getItemId(), Toast.LENGTH_SHORT).show();
                        return true;
                        //Toast.makeText(Detailed.this,"working",Toast.LENGTH_LONG).show();
                    }
                });
                popup.show();//showing popup menu
            }
            });


//the function to pick and store image how can i retrieve it onresume
@Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);

       if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
           Uri selectedImage = data.getData();
           String[] filePathColumn = { MediaStore.Images.Media.DATA };

           Cursor cursor = getContentResolver().query(selectedImage,
                   filePathColumn, null, null, null);
           cursor.moveToFirst();

           int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
           String picturePath = cursor.getString(columnIndex);
           cursor.close();


           SharedPreferences sharedPreferences = Detailed.this.getSharedPreferences(AppConfig.SHARED_PREF_NAME,
                   Context.MODE_PRIVATE);
           SharedPreferences.Editor editor = sharedPreferences.edit();
           editor.putBoolean(AppConfig.IMAGE_SHARED_PREF, true);
          editor.putString(AppConfig.IMAGE_SHARED_PREF1, selectedImage.toString());
           editor.putString(AppConfig.filepath, filePathColumn.toString());
           editor.commit();

           ImageView imageView = (ImageView) findViewById(R.id.btn_search);
           imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
//http://tjkannan.blogspot.co.ke/2012/01/load-image-from-camera-or-gallery.html
       }


   }

1 个答案:

答案 0 :(得分:0)

这是问题 -

    ImageView imageView = (ImageView) findViewById(R.id.btn_search);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

问题是您在 onActivityResult 中设置了图片,只有当您从手机图库中选择了图片或从相机拍摄图像时才会调用此图片。

1。 在 onActivityResult 中选择图像后,在SharedPreferences中保存选定的图像路径 -

   editor.putString("picturepath", filePathColumn.toString());
   editor.commit();

2。 将此代码放在 onCreate 中,以便每次返回应用时都会调用此代码 -

String pathOfImage = sharedPreferences.getString("picturepath", null);

ImageView imageView = (ImageView) findViewById(R.id.btn_search);

    if(pathOfImage.length()!=0){
     imageView.setImageBitmap(BitmapFactory.decodeFile(pathOfImage));
     }