How to send an image from one activity to another?

I need to send an image, as I send the string "title", but I can not, how can I send an image (R.drawable.image) from mainactivity to secondactivity?

thanks

PRIMARY 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); } 
+4
source share
4 answers

Solution 1: (for resources not drawable )

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

If you are having problems using the path to the ImageView file. Show image from file path?


Solution 2: (for drawable easy and easy way)

Send the value of an integer resource, for example:

PRIMARY 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); } 
+5
source

Put the image path in putExtra. Do not send a bitmap, it can be hard

+2
source

Save file path

 intent.putExtra("imagePath", filepath); 

send image using intent and use

 String image_path = getIntent().getStringExtra("imagePath"); Bitmap bitmap = BitmapFactory.decodeFile(image_path); myimageview.setImageDrawable(bitmap); 
+2
source

Binary files are available in all actions in your application.
You can directly contact them instead of sending them from one activity to another.

0
source

All Articles