Search code examples
androidandroid-recyclerviewimageviewpicasso

Assign image resource in an image view with string


(1) I am using recyleview to fetch image, text & audio file from API. For text I used volley & for image use picasso, what I will use for audio file.

(2) after fetch the image & text when apply onclicklistener pass the info to another activity. Can pass only text not images.

What I need to do to solve this problem?

this is adapter part

viewHolder.textViewStory.setText(banglaItem.getStoryName());
viewHolder.textViewWritter.setText(banglaItem.getWritterName());

        Picasso.get().load(banglaItem.getStoryImage()).fit().into(viewHolder.imageView);

        viewHolder.linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,AudioActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("storyName",banglaItem.getStoryName());
                intent.putExtra("storyImage",banglaItem.getStoryImage());

                context.startActivity(intent);
            }
        });

this is the new activity part

public class AudioActivity extends AppCompatActivity {

    TextView name;
    ImageView image;
    String nameStory;
    String imageStory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio);

        name = findViewById(R.id.audioName);
        image = findViewById(R.id.audioImage);

        nameStory = getIntent().getStringExtra("storyName");
        imageStory = getIntent().getStringExtra("storyImage");

        name.setText(nameStory);
        int resourceId = getResources().getIdentifier(imageStory,"drawable", getPackageName());
        image.setImageResource(resourceId);
    }
}

Solution

  • (1.) if you have complete url of your audio file then you can directly stream audio file using media player classes provided by android.

    (2.) you are passing storyImage in next activity ,which is a url that you get from api. so you have to find same on next activity i.e

       TextView name;
       ImageView image;
       String nameStory;
       String imageStory;
    
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_audio);
    
       name = findViewById(R.id.audioName);
       image = findViewById(R.id.audioImage);
    
       nameStory = getIntent().getStringExtra("storyName");
       imageStory = getIntent().getStringExtra("storyImage");
    
       name.setText(nameStory);
       Picasso.get().load(imageStory).fit().into(image);
       //here image story contain url that you sent through first activity. As picasso uses cache, it will load (already loaded image in first activity) very smoothly from it cache.
    }