将图像保存到SharedReferences

aemubtdh  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(358)

我需要将此裁剪的图像保存在SharedReferences中。图像将是一个应用程序壁纸

public void onChoseFiles() {
            CropImage.activity().start(MainActivity.this);

        }
   @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            ActivityResult result = getActivityResult(data);
            if (resultCode == RESULT_OK) {
                mImageUri = result.getUri();
                myImg.setImageURI(mImageUri);
            } else if (resultCode == CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception e = result.getError();
                Toast.makeText(this, "Possible Error is :" + e, Toast.LENGTH_SHORT).show();
            }
        }
    }
w7t8yxp5

w7t8yxp51#

我就是这样用的,很管用。我错过了什么?
它是这样工作的!!!
一次创建

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String mImageUri = preferences.getString("image", null);
    if (mImageUri != null) {
        myImg.setImageURI(Uri.parse(mImageUri));
    } else {
        myImg.setImageResource(R.drawable.print);
    }

public void saveData(Uri yourUri) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("image", String.valueOf(mImageUri));
        editor.commit();
    }
uajslkp6

uajslkp62#

您可以尝试将uri以字符串形式保存到共享首选项:
然后您可以实现:

private static String SHARED_PREFS = "sharedPrefs";
    private static String KEY = "wallpaperObject";

    public void saveData(Uri yourUri) {

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KEY, yourUri.toString());
        editor.apply();
    }

可通过以下方式进入:

public Uri loadData() {
        SharedPreferences sharedPreferences =  getApplicationContext().getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        String text = sharedPreferences.getString(KEY, "");
        if (!text.isEmpty()) {
            return Uri.parse(text);
        } else {
            System.err.println("Not stored");
        }
        return null;
    }

在您的实现中,它可能看起来像:

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            ActivityResult result = getActivityResult(data);
            if (resultCode == RESULT_OK) {
                mImageUri = result.getUri();
                myImg.setImageURI(mImageUri);
                saveData(mImageUri);
            } else if (resultCode == CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception e = result.getError();
                Toast.makeText(this, "Possible Error is :" + e, Toast.LENGTH_SHORT).show();
            }
        }
    }

    private static String SHARED_PREFS = "sharedPrefs";
    private static String KEY = "wallpaperObject";

       public void saveData(Uri yourUri) {

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KEY, yourUri.toString());
        editor.apply();
    }

    public Uri loadData() {
        SharedPreferences sharedPreferences =  getApplicationContext().getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        String text = sharedPreferences.getString(KEY, "");
        if (!text.isEmpty()) {
            return Uri.parse(text);
        } else {
            System.err.println("Not stored");
        }
        return null;
    }

我还将添加到activity oncreate()以加载最后一组图像:

if(loadData() != null){
myImg.setImageURI(mImageUri);
}

相关问题