在我的应用中,用户通过点击指向他们的图库的按钮来选择图片。他们选择了图片但是一旦我退出应用程序,图片已经消失了。我怎么能保存它?如何使用SharedPeferences保存它?
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
public class MyNewScreen extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_new_screen);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String picturePath = prefs.getString("profilePic", "");
if(!picturePath.equals("")){
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
Button buttonLoadImage = (Button) findViewById(R.id.button1);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
private static int RESULT_LOAD_IMAGE = 1;
@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();
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("profilePic", picturePath);
edit.commit();
}
}
}
答案 0 :(得分:0)
试试这些: 其中prefs是你的sharedpreferences对象而不是布尔值,你传给他一个字符串有值
private void savePreferences(String key, boolean value) {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putBoolean(key, value);
editor.commit();
}
private boolean loadSavedPreferences() {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
return prefs.getBoolean("hasDataToSync", false);
}
答案 1 :(得分:0)
您正在从首选项中检索路径,但不保存任何内容。当你使用startActivityForResult
时,我猜你会在onActivityResult
中找到路径,那里你应该这样做:
prefs = getPreferences(Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putSring(KEY, path);
editor.commit();
当用户启动应用时,在onCreate
中检索刚刚存储的路径:
String path = prefs.getString(KEY, "");
if (path != ""){
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(path));
}
答案 2 :(得分:0)
您的代码在我的设备上正常运行。我唯一需要补充的是:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 6;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath, options));
如果图像大到足以抛出OutOfMemory异常。如果您使用模拟器,它可能不起作用。