如何使用共享preferences保存多个图像的ImageView [英] How to Save multiple images to ImageView using Shared Preferences

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

问题描述

我有一对夫妇ImageViews的活动。用户可以长preSS一个ImageView的,他们有从相机库获得任何图像的选项。我试图拯救那些图像的ImagePath,这样当用户关闭和打开应用程序再次图像依然会在ImageView的。我不明白为什么这是行不通的。

下面是我的一个活动,其中ImageView的是

 公共无效的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中);};    @覆盖
保护无效的onPause(){
    共享preferences SP = getShared preferences(AppShared preF,1); //打开共享preferences与名AppShared preF
    编辑编辑= sp.edit();
    editor.putString(的ImagePath,selectedImagePath); //商店selectedImagePath与关键的ImagePath。该密钥将被然后用来检索数据。
    editor.commit();
    super.onPause();
    }保护无效onResume1(){
    共享preferences SP = getShared preferences(AppShared preF,1);
    selectedImagePath = sp.getString(的ImagePath,);
    super.onResume();
    }

和活动B获得凸轮画廊图片,并发送回活动A

 按钮发送=(按钮)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中);
    }


解决方案

  @覆盖
保护无效onResume(){
    共享preferences SP = getShared preferences(AppShared preF,1);
    selectedImagePath = sp.getString(的ImagePath,);
    位图MYBITMAP = BitmapFactory.de codeFILE(selectedImagePath);
    im1.setImageBitmap(MYBITMAP);
    super.onResume();
}

而不是:

 保护无效onResume1(){
    共享preferences SP = getShared preferences(AppShared preF,1);
    selectedImagePath = sp.getString(的ImagePath,);
    super.onResume();
}

由于您的活动来延长活动类,它已宣布和实施此方法。这种方法是在活动返回到前台叫。你想要的是,这样它你想要它做的事情重写此方法。

I have a activity with a couple of ImageViews. The user can LongPress an ImageView and they have the option of getting any image from the camera gallery. I am trying to save the imagepath of those images so that when the user closes and opens the app again the image will still be in the imageView. I dont understand why this isn't working.

Here is my Activity A where the imageView is

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

    @Override
protected void onPause() {
    SharedPreferences sp = getSharedPreferences("AppSharedPref", 1); // 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();
    }   

protected void onResume1() {
    SharedPreferences sp = getSharedPreferences("AppSharedPref", 1);
    selectedImagePath = sp.getString("ImagePath", "");
    super.onResume();
    }

And Activity B gets the cam gallery pic and sends back to Activity A

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

解决方案

Make:

@Override
protected void onResume() {
    SharedPreferences sp = getSharedPreferences("AppSharedPref", 1);
    selectedImagePath = sp.getString("ImagePath", "");
    Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
    im1.setImageBitmap(myBitmap);
    super.onResume();
}

instead of:

protected void onResume1() {
    SharedPreferences sp = getSharedPreferences("AppSharedPref", 1);
    selectedImagePath = sp.getString("ImagePath", "");
    super.onResume();
}

Because your activity extends Activity class, which has this method declared and implemented. This method is called after Activity returns to foreground. What you want is to override this method so that it does what you want it to do.

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

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