Home > Software design >  How to display image in second activity?
How to display image in second activity?

Time:04-29

I want to display an image from the first activity to the second activity,

The images are displayed well in the first activity

I 'm new in Android developpment.

Thanks for your help

First Activity :

// Configure item click on RecyclerView
private void configureOnClickRecyclerView() {
    ItemClickSupport.addTo(recyclerView, R.layout.fragment_resto_list)
            .setOnItemClickListener((recyclerView, position, v) -> {

                // 1 - Get restaurant from adapter
                RestaurantModel restaurant = restoListAdapter.getPlacesList().get(position);
                // 2 - Show result in a Toast
                Toast.makeText(getContext(), "You clicked on Restaurant : "   restaurant.getName(), Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(requireActivity(), RestaurantDetail.class);
                intent.putExtra("Name",restaurant.getName());
                intent.putExtra("Adress",restaurant.getVicinity());

                intent.putExtra("Photo",restaurant.getPhotos().get(0).getPhotoReference());

                startActivity(intent);
            });
}

}

I can pass the name and adress but not the image

Second Activity :

public class RestaurantDetail extends AppCompatActivity {

Context context;
ImageView logo;
List<RestaurantModel> placesList;


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

    logo = (ImageView) findViewById(R.id.logo);

    TextView Detail = (TextView) findViewById(R.id.detail_name);
    TextView Adress = (TextView) findViewById(R.id.detail_address);

    Intent callingIntent = getIntent();
    if (callingIntent != null) {
        String name = callingIntent.getStringExtra("Name");
        String adress = callingIntent.getStringExtra("Adress");

        String data = name;
        Detail.setText(data);
        String data2 = adress;
        Adress.setText(data2);
    }
}

}

CodePudding user response:

imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();

Intent intent = new Intent(this, RestaurantDetail.class);
intent.putExtra("BitmapImage", bitmap);

RestaurantDetail

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

CodePudding user response:

if restaurant.getPhotos().get(0) return Bitmap Convert it to a Byte array then add it to the intent

ByteArrayOutputStream bs = new ByteArrayOutputStream();
restaurant.getPhotos().get(0).compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent= new Intent(this, RestaurantDetail.class);
intent.putExtra("image",byteArray);
startActivity(intent);

Second Activity :

byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
  • Related