我正在做一個應用程序來將圖片從圖庫保存到ImageView中,爲此我必須保存在sharedPreference中,以便當我離開應用程序並返回時,圖像仍然存在。如何將裁剪後的圖像URI保存在saredPreference中?
PS:我已經讀到這裏用相同的標題一個問題,但犯規爲我工作
有人能幫助我嗎?請JAVA文件的
部分
++++++++++++++++++++++++
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences("data", context.MODE_PRIVATE);
imgButton = (ImageView) findViewById(R.id.AddPic);
imgButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE);
}
});
}
@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) {
SelectedImage = data.getData();
performCrop();
}
else if(requestCode == PIC_CROP){
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thePic.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] byteArray = baos.toByteArray();
String imageString= Base64.encodeToString(byteArray , Base64.DEFAULT);
byte[] imageBytes = Base64.decode(imageString.getBytes());
imgButton.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length));
}
}
private void performCrop() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(SelectedImage, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
}
catch(ActivityNotFoundException anfe){
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}}
++++++ +++++++++++++++++++
我想這一點,第一個答案:How to save cropped image uri in shared Preference 但沒有工作
也許我有位圖轉換爲字符串或做別的東西,但我不知道
個Thakns反正
感謝您的支持。我這樣做了,但是在我裁剪之後,圖像沒有出現在ImageView中。它全是白色的。可以是什麼? –
@Mustanserlqbal –
請分享您的代碼? –