我有一项活动可以打开另一项活动来获取相机库照片。图片返回到我原来的活动并在imageView中休息。这工作正常。如何保存图像,以便用户稍后返回或杀死应用程序时图像仍然存在。我知道我应该使用共享首选项来获取图像路径而不是保存图像本身,但我只是不知道是怎么做的。
活动A
private ImageView im1;
private String selectedImagePath;
private static final int SELECT_PICTURE = 1;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
im1.setImageURI(selectedImageUri);
}}}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
};
((Button)dialogView.findViewById(R.id.button3))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}});
活动B
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
setResult(RESULT_OK, intent);
Bundle bundle=new Bundle();
bundle.putInt("image",R.id.showImg);
intent.putExtras(bundle);
finish();
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}}}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
答案 0 :(得分:4)
使用图片覆盖onPause()
中的Activity
方法(了解原因onPause
,请在此处查看活动图的生命周期:http://developer.android.com/reference/android/app/Activity.html),如下所示:
@Override
protected void onPause() {
SharedPrefrences sp = getSharedPreferences("AppSharedPref", 0); // Open SharedPreferences with name AppSharedPref
Editor editor = sp.edit();
editor.putString("ImagePath", selectedImagePath); // Store selectedImagePath with key "ImagePath". This key will be then used to retrieve data.
editor.commit();
super.onPause();
}
这意味着只要此Activity
进入后台,图片路径就会保存在名为SharedPreferences
的{{1}}中 - 此名称可以是您喜欢的任何名称,但您需要使用检索数据时也是一样。
然后在同一AppSharedPref
中覆盖onResume()
方法,以便在Activity
到达前景时检索图像路径:
Activity
您可能还希望使用覆盖其他方法,例如根据图表@Override
protected void onResume() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 0);
selectedImagePath = settings.getString("ImagePath", "");
super.onResume();
}
,但我留给您。
答案 1 :(得分:1)
您可以使用“selectedImagePath”将意图从一个Activity传递到另一个Activity。
活动A中的。
Intent intent = new Intent(this,Activity.class); intent.putExtra(“imagePath”,selectedImagePath);
并在活动B中获取
String strImagePath = getIntent()。getExtras()。getString(“imagePath”);
答案 2 :(得分:1)
String imagePath = ... ;
SharedPrefrences prefs = getSharedPreferences("application_settings", 0);
Editor editor = prefs.edit();
editor.putString("image_path", imagePath);
editor.commit();
答案 3 :(得分:1)
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
阅读偏好:
String path = prefs.getString("key", default value);
编辑和保存偏好
prefs.edit().putString("key", value).commit();