我的应用程序允许用户通过从图库中选择图片或用相机拍照来设置他们的个人资料图片。我已经能够成功实现该部分,但每当应用程序恢复时,图片就会消失。我正在寻找有关如何在共享首选项中保存图像路径的帮助,以便我可以在恢复时检索图像。以下是我没有成功的尝试。
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(UsersInfo.this);
builder.setTitle("Set a profile picture");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);
viewImage.setImageBitmap(bitmap);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c=getContentResolver().query(selectedImage,filePath,null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.w("ImagePath", picturePath+"");
viewImage.setImageBitmap(thumbnail);
}
}
}
@Override
public void onResume() {
Log.d(TAG, "onResume");
SharedPreferences prefs = this.getSharedPreferences(
"edition.last.cp.mobilelife.prefs", Context.MODE_PRIVATE);
selectedImage = prefs.getString("picturePath", "");
displayUserSettings();
super.onResume();
}
@Override
protected void onPause() {
SharedPreferences prefs = this.getSharedPreferences(
"edition.last.cp.mobilelife.prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("ImagePath", selectedImage); // Store selectedImagePath with key "ImagePath". This key will be then used to retrieve data.
editor.commit();
super.onPause();
}
答案 0 :(得分:0)
创建一个名为SessionManagement的新类
public class SessionManagement {
private static final String EXTRA_IMG_PATH = "img_path";
private SharedPreferences pref;
private Editor editor;
private Context _context;
public SessionManagement(Context _context) {
super();
this._context = _context;
pref = _context.getSharedPreferences(SharedPreferencesName, privateMode);
}
public boolean getPath() {
return pref.getString(EXTRA_IMG_PATH, null);
}
public void setPath(String imagePath) {
editor = pref.edit();
editor.putString(EXTRA_IMG_PATH, imagePath);
editor.commit();
}
}
并在您的活动中:
public void savePath(String path){
SessionManagment sm = new SessionManagment(getApplicationContext());
sm.setImagePath(path);
}
public String getPath(){
SessionManagment sm = new SessionManagment(getApplicationContext());
return sm.getImagePath(sm);
}
答案 1 :(得分:0)
您可以这样做:
String x = null;
SharedPreferences spPrint = getSharedPreferences
("edition.last.cp.mobilelife.prefs", Context.MODE_PRIVATE);
String y = spPrint.getString("ImagePath", x);
然后你可以在你想要的地方设置y的值。