-2

codes for filling the imageview onClick event on the main activity

if (view.equals(findViewById(R.id.madrid_imageButton))) {
            Bitmap madridImage = BitmapFactory.decodeResource(getResources(),R.drawable.madrid);
            Intent buyProduct_Intent = new Intent(MainActivity.this,MainActivityBuy.class);
            buyProduct_Intent.putExtra("Flavor","madrid");
            buyProduct_Intent.putExtra("Description",madrid_Description);
            buyProduct_Intent.putExtra("Bitmap",madridImage);
            startActivity(buyProduct_Intent);

        } else if (view.equals(findViewById(R.id.berlin_imageButton))) {
            Bitmap berlinImage = BitmapFactory.decodeResource(getResources(),R.drawable.berlin);
            Intent buyProduct_Intent = new Intent(MainActivity.this,MainActivityBuy.class);
            buyProduct_Intent.putExtra("Flavor","berlin");
            buyProduct_Intent.putExtra("Description",berlin_Description);
            buyProduct_Intent.putExtra("Bitmap",berlinImage);
            startActivity(buyProduct_Intent);

I have 10 of the above checks and here is the code for the new Activity that passed after the above condition

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buy_product); // Load buy product screen

        TextView productNameTextView = (TextView) findViewById(R.id.productName_textView);
        TextView productDesciptionTextView = (TextView) findViewById(R.id.productDescription_textView);
        ImageView productImageView = (ImageView) findViewById(R.id.productImage_imageView);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            productNameTextView.setText(extras.getString("Flavor"));
            productDesciptionTextView.setText((extras.getString("Description")));
            Bitmap sharmImage = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
            productImageView.setImageBitmap(sharmImage);
        }

        findViewById(R.id.size_radioButton_60ml).performClick();
        findViewById(R.id.nic_radioButton_3mg).performClick();
        findViewById(R.id.size_radioButton_30ml).performClick();
    };

from this 2nd activity I need to intent a third activity that uses some of the views values. I successed to retreive all the needed values except for the imageView it displays as empty don't know why .....

here is the code that intents the third activity that contains the issue

public void onClickAddToCart(View view) {
            TextView orderDetailsTextView1 = (TextView) findViewById(R.id.productName_textView);
            TextView orderDetailsTextView2 = (TextView) findViewById(R.id.totalItems_textView);
            TextView orderDetailsTextView3 = (TextView) findViewById(R.id.totalCost_textView);
            String orderDetailsTextView = orderDetailsTextView1.getText().toString() + "  " +  orderDetailsTextView2.getText().toString() + "  " + orderDetailsTextView3.getText().toString();

            ImageView productImageView = (ImageView) findViewById(R.id.productImage_imageView);
            ImageView newImageView = new ImageView(this);
            newImageView.setImageDrawable(productImageView.getDrawable());

            int drawableId = getResources().getIdentifier(productImageView.getDrawable().toString(), "drawable", getPackageName());
            Bitmap productImage = BitmapFactory.decodeResource(getResources(),drawableId);

            Intent buyProduct_Intent = new Intent(MainActivityBuy.this, Cart.class);
            buyProduct_Intent.putExtra("OrderDetails", orderDetailsTextView);
            buyProduct_Intent.putExtra("Bitmap", newImageView.getDrawable().toString());
            startActivity(buyProduct_Intent);
        }

below are the codes of the onCreate of the third activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_cart); // Load the cart XML Screen

        TextView orderDetailsTextView = (TextView) findViewById(R.id.orderDetailsTextView);
        ImageView productImageView = (ImageView) findViewById(R.id.productImage_imageView);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            orderDetailsTextView.setText(extras.getString("OrderDetails"));
            Bitmap productImage = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
            productImageView.setImageBitmap(productImage);
        }
    }

    @Override
    public void onBackPressed() {
//        startActivity(new Intent(MainActivityBuy.this, MainActivity.class));
        finish();
    }

}

By the way there is no errors or crashes .. only blank imageview at the third activity but the 1st two activities are functioning correctly and the XMLs are fine as well ..

  • https://stackoverflow.com/questions/2459524/how-can-i-pass-a-bitmap-object-from-one-activity-to-another – ADM Feb 20 '18 at 01:02
  • Thanks a lot for your help but after going through the link you provided .. i noticed that all questions and answers are related to loading actual image file to another activity using intent but my case is different because i'm trying to save whatever drawable name is in the imageview to be retrieved withoit giving the drawable name as constant name i need it to be variable ..... – Bassam Kassem Feb 20 '18 at 01:13
  • i'm trying to save whatever drawable name is in the imageview to be retrieved withoit giving the drawable name as constant name i need it to be variable? wat did you mean by this – Bunny Feb 20 '18 at 07:13
  • I mean that i have 12 ImageButtons . And each one displays a different image . So now i have 12 files inside drawable folder.. when i press on any of the ImageButtons it intents some data to 2nd Activity. And imageButton src is one of the intended data .. so i think the solution here is to store the intended image name to string variable to be able to call it when intending the third activity .. so is it possible to store Image file name to string? – Bassam Kassem Feb 20 '18 at 09:20

1 Answers1

0

based on the brainstorming done during the above questions and comments i came up with the below idea and it actually solved the issue :)

public void onClickAddToCart(View view) {
        // Here is i concatenated three textViews values - calculations into one single string 
        // to pass it to the Cart activity
        TextView orderDetailsTextView1 = (TextView) findViewById(R.id.productName_textView);
        TextView orderDetailsTextView2 = (TextView) findViewById(R.id.totalItems_textView);
        TextView orderDetailsTextView3 = (TextView) findViewById(R.id.totalCost_textView);
        String orderDetailsTextView = orderDetailsTextView1.getText().toString();
        orderDetailsTextView +=   "  " +  orderDetailsTextView2.getText().toString();
        orderDetailsTextView +=  "  :  " + orderDetailsTextView3.getText().toString();

        // in the below declaration i saved the originally intent image source to a variable 
        // and her i recall it to re-intent it again
        Bundle extras = getIntent().getExtras();
        Bitmap productImage = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");

        // Here is the passing method - transfering below data from this activity to the Cart activity
        Intent buyProduct_Intent = new Intent(MainActivityBuy.this, Cart.class);
        buyProduct_Intent.putExtra("OrderDetails", orderDetailsTextView);
        buyProduct_Intent.putExtra("Bitmap", productImage);
        startActivity(buyProduct_Intent);
    }