Search code examples
androidandroid-intentandroid-imageviewandroid-image

Passing an image from an activity to another one


I want to pass images from one activity to another when button clicked. I have one button "Show Image" in first activity. When I click on it, it should pass two images from my mipmap folder of my project and go to second activity and show one of the passed image on the ImageView of that activity. On second activity, I have two buttons which are supposed to receive images and show those images when clicked on each button. I tried using intent to pass the image, however, it didn't work. Is there other way to send images from mipmap folder from one activity to another?

Here is my code: MainActivity.java

package com.example.abina.myapplication;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity {

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

        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showmyImage();
            }
        });

    }
    public void showmyImage(){
        Intent intent = new Intent(this, Main2Activity.class);
        Bitmap bitmap; // your bitmap
        bitmap = null;
        ByteArrayOutputStream _bs = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
        intent.putExtra("byteArray", _bs.toByteArray());
        startActivity(intent);
    }
}

activity_main.xml

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="137dp"
        android:text="Show Image" />

</RelativeLayout>

Main2Activity.java

package com.example.abina.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

public class Main2Activity extends AppCompatActivity {

    Button image1;
    Button image2;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        image1 =(Button) findViewById(R.id.image1);
        image2 = (Button) findViewById(R.id.image2);
        imageView =(ImageView) findViewById(R.id.imageView);


        if(getIntent().hasExtra("byteArray")) {
            Bitmap _bitmap = BitmapFactory.decodeByteArray(
                    getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
            imageView.setImageBitmap(_bitmap);
        }
    }
}

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">

    <Button
        android:id="@+id/image1"
        android:layout_width="199dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="Image1" />

    <Button
        android:id="@+id/image2"
        android:layout_width="183dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="0dp"
        android:text="Image2" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

Images First Image

Second Image


Solution

  • First the mipmap folders are for placing your app/launcher icons (which are shown on the homescreen) in only. Any other drawable assets you use should be placed in the relevant drawable folders.

    Next since they will be in your drawables, I would just pass the @DrawableRes id e.g. the R.id.image_name value.

    Intent intent = new Intent(this, Main2Activity.class);
    intent.putExtra(IMAGE_RES_ID_KEY, R.id.imageName);
    startActivity(intent);
    

    Also I would recommend that you use a public static variable IMAGE_RES_ID_KEY for your extra key to avoid typos.

    Then on the other side you can simply

    if(getIntent().hasExtra(MainActivity.IMAGE_RES_ID_KEY)) {
        imageView.setImageResource(getIntent().getIntExtra(MainActivity.IMAGE_RES_ID_KEY, 0));
    }