Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在SharedReferences中保存ImageButton的资源?_Java_Android_Bitmap_Sharedpreferences_Android Sharedpreferences - Fatal编程技术网

Java 如何在SharedReferences中保存ImageButton的资源?

Java 如何在SharedReferences中保存ImageButton的资源?,java,android,bitmap,sharedpreferences,android-sharedpreferences,Java,Android,Bitmap,Sharedpreferences,Android Sharedpreferences,我的应用程序中有一系列图像按钮。通过该应用程序,用户可以将ImageButton的背景设置为可绘制文件。 这工作得非常好,当用户选择他们想要的图像并单击刷新按钮时,ImageButton的图像会发生变化 问题是,当我转到其他活动时,更改的ImageButton将转到并返回到默认图像 我一直试图通过SharedReferences来实现这一点。我试着用这里所有其他类似的问题来解决这个问题,但运气不好 说到共享参考,我是一个完全的初学者,我知道存储图像不是它的目的,但这是我能想到的解决问题的唯一方法

我的应用程序中有一系列图像按钮。通过该应用程序,用户可以将ImageButton的背景设置为可绘制文件。 这工作得非常好,当用户选择他们想要的图像并单击刷新按钮时,ImageButton的图像会发生变化

问题是,当我转到其他活动时,更改的ImageButton将转到并返回到默认图像

我一直试图通过SharedReferences来实现这一点。我试着用这里所有其他类似的问题来解决这个问题,但运气不好


说到共享参考,我是一个完全的初学者,我知道存储图像不是它的目的,但这是我能想到的解决问题的唯一方法

非常感谢您的帮助!提前谢谢各位

我对使用SharedReference存储图像的了解: 我知道我必须将缺陷转换为位图,然后将其编码为Base64(因为SharedReferences将接受字符串)。我已经这样做了,我认为编码是有效的

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
grey11.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] grey11base64 = byteArrayOutputStream.toByteArray();
final String encodedGrey11 = Base64.encodeToString(grey11base64, Base64.DEFAULT);
在我的主要活动(显示图像的地方)中,我目前有OnCreate和onStop方法以及onClick刷新按钮

编辑:

在我的onCreate(Bundle savedInstantState)中:

在我的刷新按钮内:

int drawableId = sharedPref.getInt("drawableId", 0);
                    ImageButton.setBackgroundResource(drawableId)

只需将可提取id存储在SharedReferences中并引用即可

    // store drawable id in SharedPreferences
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("drawableId", R.drawable.btn_background);
    editor.commit();

    // Read from sharedPref and set background
    int drawableId = sharedPref.getInt("drawableId", 0);

    ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
    imageButton.setBackgroundResource(drawableId);

你最好不要存储可提取id,因为每次编译你的应用程序时,id都可能不同。你可以将可提取名称存储在SharedReferences中,然后通过ResourcesUtils获取这些资源id

 public class ResourcesUtils {

        private static final String RES_ID = "id";
        private static final String RES_STRING = "string";
        private static final String RES_DRABLE = "drable";

        public static int getId(Context context,String resName){
            return getResId(context,resName,RES_ID);
        }


        public static int getStringId(Context context,String resName){
            return getResId(context,resName,RES_STRING);
        }


        public static int getDrableId(Context context,String resName){
            return getResId(context,resName,RES_DRABLE);
        }

        public static int getResId(Context context,String resName,String defType){
            return context.getResources().getIdentifier(resName, defType, context.getPackageName());
        }
    }

“但这是我能想到的解决问题的唯一方法”-保存文件名、资源标识符或用于获取图像的任何内容如何?好的,谢谢您的建议!我正在使用缺点来获得图像。我能在这条路上留一条路吗?还是可提取的名称?我只是不知道如何从共享偏好中获取图像。你知道如何保存你的建议吗?然后它将保持国家的稳定。干杯@MikeM。如果你指的是应用程序中的资源可提取项,那么每个
R.drawable
实际上只是一个
int
。将其保存到
SharedReferences
,然后使用检索到的值调用
setImageResource()
,会更简单、更快。这里有一个基本的
SharedReferences
/
int
示例:。@MikeM。谢谢你,迈克。我会试一试,让你知道怎么回事!干杯,谢谢你的回答,纳文!这看起来很有希望。我现在就试试,让你知道。欢迎光临。我在发布之前测试了代码。如果有任何问题,请告诉我。谢谢,我收到一个带有“imageButton”的无法解析符号。我曾尝试将其作为ImageButton,但“setBackgroundResource”出现了一个错误,即“无法从静态上下文引用非静态方法”。可能是因为我用“this”替换了代码中的“context”。有什么想法吗?干杯首先,您必须获取ImageButton的实例,然后使用setBackgroundResource。ImageButton ImageButton=(ImageButton)findViewById(R.id.ImageButton);imageButton.setBackgroundResource(drawableId);让我们。
 public class ResourcesUtils {

        private static final String RES_ID = "id";
        private static final String RES_STRING = "string";
        private static final String RES_DRABLE = "drable";

        public static int getId(Context context,String resName){
            return getResId(context,resName,RES_ID);
        }


        public static int getStringId(Context context,String resName){
            return getResId(context,resName,RES_STRING);
        }


        public static int getDrableId(Context context,String resName){
            return getResId(context,resName,RES_DRABLE);
        }

        public static int getResId(Context context,String resName,String defType){
            return context.getResources().getIdentifier(resName, defType, context.getPackageName());
        }
    }