1

I am trying to change the background layout, using the picked gallery image. What I have so far is I am picking images from the gallery and i am sending string using the intent to MainActivity. But in Activity, Glide not working at all. In debugging mode, I don't have any exception.

First Activity

private void openGallery() {
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        Uri imageUri = data.getData();

        SharedPreferences sharedPreferences = getSharedPreferences("PREFS_BACK_DISK", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("backgroundFromDisk", String.valueOf(imageUri));
        editor.apply();
        Intent intent = new Intent(Main.this, SettingsActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        Toast.makeText(Main.this, "Theme changed", Toast.LENGTH_LONG).show();
    }
}

Second Activity (onCreate() method)

SharedPreferences prefs3 = getSharedPreferences("PREFS_BACK_DISK", MODE_PRIVATE);
String stringBackgroundFromDisk = prefs3.getString("backgroundFromDisk", "");
if (stringBackgroundFromDisk.isEmpty() || (stringBackgroundFromDisk == null)) {
    
} else {
    Glide.with(this)
            .load((stringBackgroundFromDisk))
            .into(new CustomTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource,
                                            @Nullable Transition<? super Drawable> transition) {

                    ConstraintLayout constraintLayout = findViewById(R.id.contraint_layout);
                    constraintLayout.setBackground(resource);
                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }
            });
}
Son Truong
  • 13,661
  • 5
  • 32
  • 58
kohinor
  • 33
  • 2

1 Answers1

0

It seems when an image is picked, getData() does not return the actual file location. Refer to: Get filepath and filename of selected gallery image in Android