如何使用共享preferences保存图片的路径 [英] How to save image path using Shared Preferences

查看:147
本文介绍了如何使用共享preferences保存图片的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有打开另一个活动,即可找到摄像头PIC画廊的活动。画面又回到我原来的活动和休息在ImageView的。这是工作的罚款。如何保存图像,这样,当用户回来后,​​或杀死APP的图像仍然存在。我知道我应该使用共享preferences得到图片的路径,而不是保存图像本身,而是我只是不知道如何做到这一点。

活动A

 私人ImageView的IM1;
私人字符串selectedImagePath;
私有静态最终诠释SELECT_PICTURE = 1;公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == RESULT_OK){
    如果(要求code == SELECT_PICTURE){
    乌里selectedImageUri = data.getData();
    selectedImagePath =的getPath(selectedImageUri);
    的System.out.println(映像路径:+ selectedImagePath);
    im1.setImageURI(selectedImageUri);
    }}}
公共字符串的getPath(URI URI){
    的String [] =投影{MediaStore.Images.Media.DATA};
    光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
    INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    返回cursor.getString(Column_Index中);
    };
   ((按钮)dialogView.findViewById(R.id.button3))
   .setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
    意向意图=新的Intent();
    intent.setType(图像/ *);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(意向,选择图片),SELECT_PICTURE);
    }});

活动B

 按钮发送=(按钮)findViewById(R.id.send);
    send.setOnClickListener(新OnClickListener(){
 公共无效的onClick(视图v){
            意向意图=新的Intent();
            的setResult(RESULT_OK,意向);
            束束=新包();
            bundle.putInt(形象,R.id.showImg);
            intent.putExtras(包);
            完(); 公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
         如果(结果code == RESULT_OK){
         如果(要求code == SELECT_PICTURE){
            乌里selectedImageUri = data.getData();
            selectedImagePath =的getPath(selectedImageUri);
            的System.out.println(映像路径:+ selectedImagePath);
            img.setImageURI(selectedImageUri);
        }}}公共字符串的getPath(URI URI){
    的String [] =投影{MediaStore.Images.Media.DATA};
    光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
    INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    返回cursor.getString(Column_Index中);
    }


解决方案

活动覆盖的onPause()用图像(理解为什么的onPause ,检查活动图的生命周期在这里:的 http://developer.android.com/reference/android/app/Activity.html )是这样的:

  @覆盖
保护无效的onPause(){
    共享prefrences SP = getShared preferences(AppShared preF,0); //打开共享preferences与名AppShared preF
    编辑编辑= sp.edit();
    editor.putString(的ImagePath,selectedImagePath); //商店selectedImagePath与关键的ImagePath。该密钥将被然后用来检索数据。
    editor.commit();
    super.onPause();
}

这意味着,每当这个活动进入的背景下,图像路径将被保存在共享preferences 与名称 AppShared preF - 这个名字可以是任何你喜欢的,但你需要检索数据时使用的同一个

然后重写 onResume()方法在同一活动,这样就可以检索图像路径时,活动谈到前景:

  @覆盖
保护无效onResume(){
    共享preferences SP = getShared preferences(AppShared preF,0);
    selectedImagePath = settings.getString(的ImagePath,);
    super.onResume();
}

您可能还需要与压倒一切的其他方法来打,例如像在onStart()根据图,但是这我离开你。

I have an activity that opens another activity to get a camera gallery pic. The picture comes back to my original activity and rest in an imageView. That's working fine. How do I save the image so when the user comes back later, or kills to app the image is still there. I know I am supposed the use Shared Preferences to get the image path and not save the image itself but I just don't know how do that.

Activity 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);
    }});

Activity 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);
    }

解决方案

Override onPause() method in Activity with an image (to understand why onPause, check life cycle of an Activity diagram here: http://developer.android.com/reference/android/app/Activity.html) like this:

@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();
}

It means that whenever this Activity goes into background, the image path will be saved in SharedPreferences with name AppSharedPref - this name can be whatever you like, but you need to use the same one when retrieving data.

Then override onResume() method in the same Activity so that you can retrieve image path when Activity comes to foreground:

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

You may also want to play with overriding other methods, like for example onStart() according to diagram, but this I leave to you.

这篇关于如何使用共享preferences保存图片的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆