I transferred an image from one imageview to another imageview in another

Asked

Viewed 320 times

0

Guys I have a grid view that displays images of a server, when clika in the image I have to reload the image a few more details, I wanted instead of having to download the image again, That is, I wanted to move the image from the already loaded imageview to another imageview. A Gridview

   lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                //String pid = ((TextView) view.findViewById(R.id.pid)).getText().toString();

                String pid = productsList.get(position).getPid();

                  ImageView iv = (ImageView) view.findViewById(R.id.flag);
                  Bitmap bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();

                        DetailFragment f = new DetailFragment();
                        Bundle args = new Bundle();

                        args.putParcelable("SEU_BITMAP", bitmap);

                        f.setArguments(args);

                boolean singlepane = MainActivity.getPane();
                if(singlepane== true){
                    /*
                     * The second fragment not yet loaded. 
                     * Load DetailFragment by FragmentTransaction, and pass 
                     * data from current fragment to second fragment via bundle.
                     */



                    DetailFragment detailFragment = new DetailFragment();
                    Fragment myListFragment = getFragmentManager().findFragmentByTag("ListFragment");

                    Bundle bundle = new Bundle();
                    bundle.putString("TAG_PID",pid);
                    detailFragment.setArguments(bundle);

                    FragmentTransaction fragmentTransaction =
                            getActivity().getFragmentManager().beginTransaction();

                    fragmentTransaction.setCustomAnimations(
                            R.anim.fadein, R.anim.fadeout, R.anim.fadein, R.anim.fadeout);

                    //This could use some improvement, but it works, hide current fragment, show new one
                    fragmentTransaction.hide(myListFragment);
                    fragmentTransaction.add(R.id.phone_container, detailFragment);
                    //fragmentTransaction.show(myDetailFragment);


                    /*
                     * Add this transaction to the back stack. 
                     * This means that the transaction will be remembered after it is 
                     * committed, and will reverse its operation when later popped off 
                     * the stack.
                     */
                    fragmentTransaction.addToBackStack(null);

                    fragmentTransaction.commit();

The Second Party

getActivity().runOnUiThread(new Runnable() {
                                public void run() {

                                    //creating the variables
                                    String description;
                                    String name;
                                    String tags;
                                    String downloads;
                                    String created;
                                    try {
                                        //filling the variables with json content
                                        url = (product.getString(TAG_URL));
                                        //description = (product.getString(TAG_DESCRIPTION));
                                        name = (product.getString(TAG_NAME));
                                        tags = (product.getString(TAG_TAGS));
                                        downloads = (product.getString(TAG_DOWNLOADS));
                                        created = (product.getString(TAG_CREATED));

                                        // Getting the tags in a gridview
                                        stringArray = tags.split(",");

                                        for (int i = 0; i < stringArray.length; i++)
                                            stringArray[i] = stringArray[i].trim();


                                        mylistview = (ExpandableHeightGridView) getActivity().findViewById(R.id.listViewTags);

                                        ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getActivity(),
                                                R.layout.tag_gridview_item, R.id.tagbutton,
                                                stringArray);
                                        mylistview.setAdapter(listAdapter);                                   
                                        Bitmap bitmap = getArguments().getParcelable("SEU_BITMAP");

                                        imageView.setImageBitmap(bitmap);

1 answer

1


You can retrieve the Bitmap of ImageView and pass the argument via Arguments class Fragment. The class Bitmap implements the interface Parcelable, soon it is possible to pass it to a Bundle.

To recover the Bitmap of a ImageView just use:

Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();

To pass via Intent to another Fragment:

SeuFragment f = new SeuFragment();
Bundle args = new Bundle();

args.putParcelable("SEU_BITMAP", bitmap);

f.setArguments(args);

To recover in SeuFragment:

Bitmap bitmap = getArguments().getParcelable("SEU_BITMAP");

And to set the Bitmap to his ImageView:

ImageView imageView = ...;

imageView.setImageBitmap(bitmap);
  • Turns out The putExtra method(String, Bitmap) is Undefined for the type Bundle

  • My mistake, the correct method is putParcelable, corrected in response.

  • Ta passing a null image, shows nothing.

  • Could you include in the question how was the code?

  • I put a part.

  • @Alissonhirle, from what I saw of the code you instance the DetailFragment twice, once with the variable f, that has Bitmap and another with the variable detailFragment who doesn’t have the Bitmap and then ends up just using the detailFragment to do the replace.

  • It worked, thank you.

Show 2 more comments

Browser other questions tagged

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