How to pass file from drawable as parameter?

0

I have a problem that should be simple, I want to move to another activity, a drawable as a parameter.

Here's my Onclick function:

img1.setOnClickListener(new View.OnClickListener(){
    @Override
       public void onClick(View v){
        Intent it = new Intent(List.this, StoryActivity.class);
        Drawable d = getResources().getDrawable();
        it.putExtras();
        startActivity(it);
        }
    });

For example, I have an image file called img1.jpg.

I want this function to pass to the other activity this parameter, so that as soon as the second activity is triggered it will know which file to display.

    
asked by anonymous 30.11.2016 / 00:58

1 answer

1

Instead of passing Drawable as a parameter, you can pass only the Resource ID and then instantiate Drawable on the other activity using this Resource ID. It is good to know that passing an object from one activity to another requires the execution of Serialization . , which can be costly depending on the size and complexity of the serialized object.

What you can do is the following:

In your activity you will send the drawable

img1.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v){

        Intent it = new Intent(List.this, StoryActivity.class);
        int drawableId = //R.drawable.meu_drawable_id
        it.putExtra("drawable_id", drawableId);
        startActivity(it);
    }
});

In your activity you will receive drawable

@Override
protected void onCreate(){

    /*...*/

    Intent intent = getIntent();
    int drawableId = intent.getIntExtra("drawable_id", 0); // 0 é apenas um valor default caso o drawableId não seja passado

    Drawable meuDrawable = getResources().getDrawable(drawableId);

   /*...*/
}
    
30.11.2016 / 01:19