title: How to save images uri to room database properly?
body: <p>In my app I have a recyclerview with images and initially I want some photos already to be there. They are stored in Drawable folder. Also there is a possibility for a user to add his own images from a gallery. So I came to a conclusion that I need a database for doing this. Since pictures in drawable folder have only "<strong>resource_id</strong>" for setting them into ImageView and photos in gallery have only "<strong>uri</strong>", I decided to make two variables: <em>resource_id</em> and <em>uri</em> accordingly. When pictures with <strong>resource_id</strong> will be loaded, they will have "<strong>uri</strong>" as null and I will check for that. With photos from gallery - the same but vice versa. </p>
  
  <p>The problem is that when I save a photo from gallety, instead of the photo itself I see a little dot and nothing more. It doesn't load for some reason... </p>
  
  <p>Here is the <strong>Images</strong> class:</p>
  
  <pre><code>@Entity
  public class Images {
  
  @PrimaryKey(autoGenerate = true)
  int id;
  
  int resourceId;
  
  String url;
  
  String type;
  
  public Images(int resourceId, String url, String type) {
      this.resourceId = resourceId;
      this.url = url;
      this.type = type;
  }
  
  // getters and setters
  }
  </code></pre>
  
  <p><code>Type</code> variable is something that is not relevant to my problem. </p>
  
  <p>Class of database (where some images are already loaded):</p>
  
  <pre><code>@Database(entities = {Images.class}, version = 1, exportSchema = false)
  public abstract class ImagesDatabase extends RoomDatabase {
  
  private static ImagesDatabase instance;
  
  public abstract ImagesDao imagesDao();
  
  public static synchronized ImagesDatabase getInstance(Context context) {
      if (instance == null) {
          instance = Room.databaseBuilder(context.getApplicationContext(),
                  ImagesDatabase.class, "images_database")
                  .fallbackToDestructiveMigration()
                  .addCallback(roomCallback)
                  .build();
      }
      return instance;
  }
  
  private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback(){
      @Override
      public void onCreate(@NonNull SupportSQLiteDatabase db) {
          super.onCreate(db);
          new PopulateDBAsyncTask(instance).execute();
      }
  };
  
  private static class PopulateDBAsyncTask extends AsyncTask&lt;Void, Void, Void&gt; {
  
      private ImagesDao imagesDao;
  
      private PopulateDBAsyncTask(ImagesDatabase db){
          imagesDao = db.imagesDao();
      }
  
      @Override
      protected Void doInBackground(Void... voids) {
          imagesDao.insert(new Images(R.drawable.regular1, null, "Regular"));
          imagesDao.insert(new Images(R.drawable.regular2, null, "Regular"));
          imagesDao.insert(new Images(R.drawable.regular3, null, "Regular"));
          imagesDao.insert(new Images(R.drawable.vertical2, null, "Vertical"));
          imagesDao.insert(new Images(R.drawable.vertical3, null, "Vertical"));
          imagesDao.insert(new Images(R.drawable.vertical4, null, "Vertical"));
          imagesDao.insert(new Images(R.drawable.vertical5, null, "Vertical"));
          imagesDao.insert(new Images(R.drawable.vertical6, null, "Vertical"));
          imagesDao.insert(new Images(R.drawable.vertical7, null, "Vertical"));
          imagesDao.insert(new Images(R.drawable.vertical8, null, "Vertical"));
          imagesDao.insert(new Images(R.drawable.vertical9, null, "Vertical"));
          return null;
      }
  }
  }
  </code></pre>
  
  <p>Activity, where I have a <strong>recyclerView</strong>:</p>
  
  <pre><code>public class FormsActivity extends AppCompatActivity {
  
  public static final int GALLERY_OPEN = 123;
  
  private RecyclerView recyclerView;
  private RecyclerView.LayoutManager layoutManager;
  private FormsAdapter adapter;
  private String chosenByUser;
  private Intent intent;
  private ImagesViewModel viewModel;
  private FloatingActionButton btn_add_image;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_forms);
      setTitle("Choose the type");
  
      getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
  
      recyclerView = findViewById(R.id.recycler_view);
      btn_add_image = findViewById(R.id.btn_add_image);       
  
      layoutManager = new GridLayoutManager(this, 2);
      //layoutManager = new LinearLayoutManager(this);
      recyclerView.setLayoutManager(layoutManager);
      recyclerView.setHasFixedSize(true);
  
      intent = getIntent();
      chosenByUser = intent.getStringExtra("chosenByUser");
  
      viewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(ImagesViewModel.class);
      try {
          viewModel.getAllImagesWhereTypeIs(chosenByUser).observe(this, new Observer&lt;List&lt;Images&gt;&gt;() {
              @Override
              public void onChanged(List&lt;Images&gt; images) {
                  adapter = new FormsAdapter(FormsActivity.this, images);
                  recyclerView.setAdapter(adapter);
              }
          });
      } catch (ExecutionException e) {
          e.printStackTrace();
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  
      btn_add_image.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              openGallery();
          }
      });        
  }
  
  private void openGallery() {
      Intent intent = new Intent();
      intent.setType("image/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);
      startActivityForResult(Intent.createChooser(intent, "Pick an image"), GALLERY_OPEN);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == GALLERY_OPEN &amp;&amp; resultCode == RESULT_OK &amp;&amp; data != null) {
          Uri imageData = data.getData();
          viewModel.insert(new Images(0, imageData.toString(), chosenByUser));
      }
  }
  }
  </code></pre>
  
  <p><strong>Adapter</strong> class:</p>
  
  <pre><code>public class FormsAdapter extends RecyclerView.Adapter&lt;FormsAdapter.ViewHolder&gt;{
  
  private List&lt;Images&gt; images = new ArrayList&lt;&gt;();
  private Context mContext;
  
  public FormsAdapter(Context context, List&lt;Images&gt; images2){
      mContext = context;
      images = images2;
  }
  
  @NonNull
  @Override
  public FormsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_form_layout, parent, false);
      return new ViewHolder(view);
  }
  
  @Override
  public void onBindViewHolder(@NonNull FormsAdapter.ViewHolder holder, int position) {
      Images image = images.get(position);
      final String uri = image.getUrl();
      final int resourceId = image.getResourceId();
      if (uri == null &amp;&amp; resourceId != 0){
          holder.image.setImageResource(resourceId);
      }
      else if (resourceId == 0 &amp;&amp; uri != null){
         // holder.image.setImageURI(Uri.parse(uri));
          Picasso.get().load(Uri.parse(uri)).fit().centerInside().into(holder.image);
      }
      holder.cardView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent = new Intent(mContext, MakingActivity.class);
              intent.putExtra("image_id", resourceId);
              intent.putExtra("image_uri", uri);
              mContext.startActivity(intent);
          }
      });        
  }
  
  @Override
  public int getItemCount() {
      return images.size();
  }
  
  public static class ViewHolder extends RecyclerView.ViewHolder{
  
      ImageView image;
      CardView cardView;
  
      public ViewHolder(@NonNull View itemView) {
          super(itemView);
  
          image = itemView.findViewById(R.id.my_image);
          cardView = itemView.findViewById(R.id.cardView);
      }
  }
  }
  </code></pre>
  
  <p>There is an OnClickListener in Adapter, because when the user clicks at any photo I want to open another <strong>activity</strong> and show that picture in full screen.</p>
  
  <p>So this is the <strong>activity</strong>:</p>
  
  <pre><code>public class MakingActivity extends AppCompatActivity {
  
  Intent intent;
  int image_id = 0;
  String image_uri = null;
  ImageView image;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_making_flowerbed);
  
      getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
      image = findViewById(R.id.image_make);
  
      intent = getIntent();
      image_id = intent.getIntExtra("image_id", R.drawable.ic_launcher_foreground);
      image_uri = intent.getStringExtra("image_uri");     
      if (image_uri == null &amp;&amp; image_id != 0) {            
          Picasso.get()
                  .load(image_id)
                  .fit()
                  .centerInside()
                  .into(image);
      }
      else if (image_uri != null &amp;&amp; image_id == 0){         
          Picasso.get()
                  .load(image_uri)
                  .fit()
                  .centerInside()
                  .into(image);
      }
  }
  }
  </code></pre>
  
  <p>In theory I thought this way should work. Could you please help me to solve the problem? Thank you in advance.</p>