【问题标题】:How to save image path using Shared Preferences如何使用共享首选项保存图像路径
【发布时间】:2013-06-28 13:28:55
【问题描述】:

我有一个活动可以打开另一个活动以获取相机图库图片。图片回到我原来的活动并在 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);
    }

【问题讨论】:

    标签: android imageview sharedpreferences mediastore


    【解决方案1】:

    用图像覆盖Activity 中的onPause() 方法(要了解为什么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中,名称为AppSharedPref - 这个名称可以是任何你喜欢的名称,但你需要在检索数据时使用相同的名称.

    然后在同一个Activity 中重写onResume() 方法,以便在Activity 进入前台时检索图像路径:

    @Override
    protected void onResume() {
        SharedPreferences sp = getSharedPreferences("AppSharedPref", 0);
        selectedImagePath = settings.getString("ImagePath", "");
        super.onResume();
    }
    

    您可能还想使用覆盖其他方法,例如根据图表onStart(),但我留给您。

    【讨论】:

    • 我试图在我的代码中实现这一点,但我必须把它放在错误的地方。 onPause 和 OnResume 部分不是紧跟在 onActivityResult 部分之后吗?
    • 不管你把它放在哪里,它都必须在Activity里面。是的,它可以在onActivityResult() 之后。这有什么问题?
    • 我将此标记为正确答案,因为即使我无法得到它,我也知道这是正确的方法。我用你给我的东西发布了另一个问题。还是不能保存,也许你可以帮忙。 stackoverflow.com/questions/17358820/…
    • 谢谢。我在那里为你写了一条评论。
    【解决方案2】:

    您可以使用“selectedImagePath”将意图从一个 Activity 传递到另一个。

    在活动 A 中。

    Intent 意图 = new Intent(this , Activity.class); intent.putExtra("imagePath", selectedImagePath );

    并在活动 B 中获得它,

    String strImagePath = getIntent().getExtras().getString("imagePath");

    【讨论】:

      【解决方案3】:
      String imagePath = ... ;
      
      SharedPrefrences prefs = getSharedPreferences("application_settings", 0);
      Editor editor = prefs.edit();
      editor.putString("image_path", imagePath);              
      editor.commit();
      

      【讨论】:

        【解决方案4】:
        SharedPreferences prefs = this.getSharedPreferences(
          "com.example.app", Context.MODE_PRIVATE);
        

        阅读偏好:

        String path = prefs.getString("key", default value); 
        

        编辑和保存首选项

        prefs.edit().putString("key", value).commit();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-31
          相关资源
          最近更新 更多