Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send image from one activity to other?

I need send a imageview like i send the string "title", but i can't, how can i send a imageview (R.drawable.image) from mainactivity to secondactivity?

THANKS

MAIN ACTIVITY

public void NewActivity(View view){ 
Intent intent = new Intent(this, SecondActivity.class); 
intent.putExtra("title", getString(R.string.title));    
startActivity(intent); 
} 

SECOND ACTIVITY

@Override 
public void onCreate(Bundle bundle) 
{ 

super.onCreate(bundle); 
setContentView(R.layout.pantalla); 
Bundle extras = getIntent().getExtras(); 
if (extras == null) 
{ 
return; 
} 
String title = extras.getString("title"); 

TextView textview = (TextView)findViewById(R.id.titulo); 

textview.setText(title); 
}
like image 338
oscargc Avatar asked Apr 03 '13 08:04

oscargc


2 Answers

Solution 1: (for non drawable resources)

You can send the path filename as a string. Just like the "title" in your example.

If you have problem with usage of filepath to ImageView. Show Image View from file path?


Solution 2: (for drawable easy & light way)

Send the resource integer value like:

MAIN ACTIVITY

Intent intent = new Intent(this, SecondActivity.class); 
intent.putExtra("resourseInt", R.drawable.image);    
startActivity(intent); 

SECOND ACTIVITY

@Override 
public void onCreate(Bundle bundle) 
{ 

    super.onCreate(bundle); 
    setContentView(R.layout.pantalla); 
    Bundle extras = getIntent().getExtras(); 
    if (extras == null) 
    { 
        return; 
    } 
    int res = extras.getInt("resourseInt"); 

    ImageView view = (ImageView) findViewById(R.id.something); 

    view.setImageResourse(res); 
}
like image 115
madlymad Avatar answered Sep 18 '22 21:09

madlymad


Put the path of the picture in putExtra. Do not send bitmap it may be heavy

like image 40
Nitin Gupta Avatar answered Sep 21 '22 21:09

Nitin Gupta