Transfer an image from one imageview to another imageview in another fragment

0

I have a grid view that displays images of a server, when I click on the image I have to reload the image again a few more details, I wanted instead to have to download the image again, ie I wanted to pass the imageview image already loaded into 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 fragment

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);
    
asked by anonymous 05.03.2015 / 03:28

1 answer

1

You can retrieve Bitmap from ImageView and go through argument via Arguments of class Fragment . The Bitmap class implements the Parcelable interface, so it can be passed to Bundle .

To recover Bitmap from 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 on SeuFragment :

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

And to set Bitmap to its ImageView :

ImageView imageView = ...;

imageView.setImageBitmap(bitmap);
    
05.03.2015 / 04:37