2016-03-05 20 views
0

我想要获取用户图像并存储图像,以便当用户再次打开该应用程序时,它会找到其用户个人资料图片。我可以选择图像,但破坏了应用程序后不能再查看它,然后再次打开它在android中更新后存储用户profle图片

//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 
     } 


    } 
+0

您必须将它存储在服务器端,因为如果将它保存在本地设备上的文件夹中,SharedPref或与设备相关的任何东西在卸载应用程序后已经消失。 – Ajinkya

回答

0

这是问题行 -

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

的问题是,你要设置你的形象在onActivityResult仅当您从手机图库中选择图像或从相机拍摄图像时才会调用该功能。

1. 保存在SharedPreferences一旦你onActivityResult选择图像中选取的路径 - 在的onCreate使

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

2. 将此代码somehwere是叫你每次回到您的应用程序 -

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

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

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

它解决了你的问题吗? – Ash