2015-01-21 4 views
0

내 앱에서 가져온 SharedPreferences에 사진을 저장하려고합니다. 하지만 그걸 도와 줄 수있는 코드는 없습니다. 제발 도와주세요.Android : StorePreferences의 공유 앱

public class RegisterActivity extends Activity { 

    private final int REQUEST_IMAGE = 100; 

    EditText nameEditText; 
    EditText phoneEditText; 
    EditText emailEditText; 
    EditText bdayEditText; 
    ImageView photoImageView; 
    ImageButton cameraButton; 
    Button saveButton; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_register); 

     // String nameEt = nameEditText.getText().toString(); 

     // String phoneEt = phoneEditText.getText().toString(); 

     // String emailEt = emailEditText.getText().toString(); 

     // String bdayEt = bdayEditText.getText().toString(); 

     photoImageView = (ImageView) findViewById(R.id.photoImageView); 

     cameraButton = (ImageButton) findViewById(R.id.cameraImageButton); 
     cameraButton.setOnClickListener(cameraButtonListener); 

     saveButton = (Button) findViewById(R.id.saveButton); 
     saveButton.setOnClickListener(saveButtonListener); 

    } 

    private OnClickListener cameraButtonListener = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

      startActivityForResult(intent, REQUEST_IMAGE); 

     } 
    }; 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) { 
      Bitmap userImage = (Bitmap) data.getExtras().get("data"); 

      photoImageView.setImageBitmap(userImage); 
     } 
    }; 

    private OnClickListener saveButtonListener = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      SharedPreferences sharedPreferences = getSharedPreferences(
        "WaiterData", Context.MODE_PRIVATE); 
      SharedPreferences.Editor editor = sharedPreferences.edit(); 

      nameEditText = (EditText) findViewById(R.id.nameEditText); 
      phoneEditText = (EditText) findViewById(R.id.phoneEditText); 
      emailEditText = (EditText) findViewById(R.id.emailEditText); 
      bdayEditText = (EditText) findViewById(R.id.bdayEditText); 

      if (nameEditText.getText().length() > 0 
        && phoneEditText.getText().length() > 0 
        && emailEditText.getText().length() > 0 
        && bdayEditText.getText().length() > 0) { 

       editor.putString("name", nameEditText.getText().toString()); 
       editor.putString("phone", phoneEditText.getText().toString()); 
       editor.putString("email", emailEditText.getText().toString()); 
       editor.putString("bday", bdayEditText.getText().toString()); 

       // move back to MainActivity 
       Intent intent = new Intent(RegisterActivity.this, 
         MainActivity.class); 
       startActivity(intent); 

      } else { 
       Toast.makeText(RegisterActivity.this, 
         "Please fill all The fields", Toast.LENGTH_SHORT) 
         .show(); 

      } 

     } 
    }; 

} 

다른 활동에서 코드를 어떻게 얻을 수 있습니까?

+2

이미지 저장 경로 –

+0

저장 위치 : Intent (MediaStore.ACTION_IMAGE_CAPTURE); ? –

+0

갤러리에 저장된 이미지 파일 경로를 캡처 할 사람이 없습니다. –

답변

0

이미지가 해당 문자열을 검색하고 비트 맵에 다시 디코딩이 필요한 때 그때 그 문자열을 저장할 문자열로 이미지 비트 맵을 인코딩하는 SharedPreferences이다 사용하여 이미지를 저장하는 데 사용하고있어 방법 :

// declaring the sharedPreferences 
SharedPreferences sharedPreferences = PreferenceManager 
        .getDefaultSharedPreferences(this); 
// encoding image bitmap to String 
int size = userImage.getWidth() * userImage.getHeight(); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(
            size); 
userImage.compress(Bitmap.CompressFormat.JPEG, 50, 
           stream); 
byte[] b = stream.toByteArray(); 
Editor editor = sharedPreferences.edit(); 
editor.putString("userImage", encodedImage); 
editor.commit(); 
stream.close(); 
stream = null; 

// then when needing that image again from the SharedPreferences 
String decoded_image = sharedPreferences.getString("userImage", "default"); 
try{ 
    byte[] decodedString = Base64.decode(decoded_image, Base64.DEFAULT); 
    Bitmap stored_userImage = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    // do whatever you want with it now 
}catch(OutOfMemoryError oom){ 

}