silentw
silentw

Reputation: 4885

Applying drawable to an ImageView from another Activity Layout

Using this library, I'm trying to retrieve the edited image and place it on another activity's ImageView...

Calling the function to set the edited drawable to the other ImageView:

Log.d("eiDR",gImageView.getDrawable().toString());
PreviewPostal pp = new PreviewPostal();
pp.setImage(gImageView.getDrawable());

Setting the edited drawable to the other ImageView (in PreviewPostal Activity):

public void setImage(Drawable dr){
    Log.d("ppDR",dr.toString());
    //ImageView iv = (ImageView)this.findViewById(R.id.imageForTest);
    //iv.setImageDrawable(dr);
}

This logs the same drawable, but if I uncomment those two lines, it gives me a NPE.

Note: The activities are wrapped in a TabHost (each activity are a tab with their own layouts).

Thanks in advance!

Edit: How I add the activities (tabs):

mTabHost = getTabHost();

// Tab Editar Imagem
TabSpec editImageSpec = mTabHost.newTabSpec("Imagem");
editImageSpec.setIndicator(setTabIndicator(getResources().getDrawable(R.drawable.tab_editimage_icon)));
Intent editImageIntent = new Intent(this, EditImage.class);
editImageIntent.putExtra("imagem", getIntent().getStringExtra("imagem"));
editImageSpec.setContent(editImageIntent);

Upvotes: 0

Views: 696

Answers (2)

ranjeet wagh
ranjeet wagh

Reputation: 306

Options:

1.You can start activity for result and on result of the called activity just return the byte[] of the drawable back to the calling activity

2.Have a pre-defined location on SD card,then save the image from called activity at that location and access the same location from calling activity

3.Write a simple pojo which implements serializable.Then start second activity for result and on result of second activity populate the pojo and return it from called activity to calling activity.

I hope this helps..

Upvotes: 1

Andro Selva
Andro Selva

Reputation: 54322

You cannot do this simply. First let me explain you the meaning of this line.

ImageView iv = (ImageView)this.findViewById(R.id.imageForTest);

This simply means, you are trying to refer to a element which is present in your current Activity. That is, the layout which you could have provided by using setContentView. So now when android searches for this ImageView in the current layout , obviously it will not exist and throw you a Null Pointer Exception.

What you actually have to do is, save the Drawable by some means and later when you pass to that activity, you have to set the Drawable to that ImaegView.

Refer this link, on how to convert your Drawable to Bitmap and send it to next activity using putExtra.

https://stackoverflow.com/a/9033864/603744

Upvotes: 1

Related Questions