Android - How to transport picture information from one class to another?

Asked

Viewed 165 times

0

Hello, I need to transport the contents of an Imageview from one class to another, I tried by Intent, but I couldn’t get through it. I have a Drawable that was edited in Imageview of the first class and I want to transport this already edited content to another Imageview of another class.

I tried this way as shown below, but it did not work..

In first class:

public void next(View v){
    //resultView é uma ImageView
    Bitmap p = drawableToBitmap(resultView.getDrawable());

    Bundle param = new Bundle();
    param.putParcelable("BITMAP", p);

    Intent intent = new Intent(this, EditImage.class);
    intent.putExtras(param);
    startActivity(intent);
}

In second class:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        setContentView(R.layout.activity_main);

        Intent intent = getIntent();
        Bundle param = intent.getExtras();
        Bitmap  bit = param.getParcelable("BITMAP");
        resultView.setImageBitmap(bit);
...
}

Thanks in advance for the help and attention, if anyone has any idea or hint of how to do please inform, all help is valid. Hug!

  • 1

    These images you want to carry, are inside your application?

  • No, they could be a photo the person just took, or one from the gallery but containing edits. I tried to convert to Bitmap and send, as the example of the question shows, but I was not successful.

  • 1

    What do you mean "it didn’t work out"? There’s some mistake?

  • Yes, the app stops working with the error (Unfortunately, Namesepp has stopped) Android Studio reports that the error occurs in this line of code ( resultView.setImageBitmap(bit); )

1 answer

1


Guys, I kept trying here and I got it this way :)

In Class 1:

public void next(View v){
    Bitmap p = drawableToBitmap(resultView.getDrawable());

    Bundle param = new Bundle();
    param.putParcelable("BITMAP", p);

    Intent intent = new Intent(this, Classe2.class);
    intent.putExtras(param);
    startActivity(intent);
}

In Class 2:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        Bundle param = intent.getExtras();
        Bitmap  bit = param.getParcelable("BITMAP");
        Drawable drawable=new BitmapDrawable(bit);
        resultView.setImageDrawable(drawable);
        ...
}

Thanks for your help and attention! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.