Pick selected image and send to another Activity

Asked

Viewed 421 times

0

I wonder if it is possible to take a selected image and send it to another Activity. I’m using Fragments and wanted the layout to look like the image below.inserir a descrição da imagem aqui

Wanted that when the user selects the image, pick this and send to another Activity and present on the screen. Class Fragment:

public class Fragment1 extends Fragment {

GridView gridView;
int [] listaImagens = new int[]{R.drawable.eu, R.drawable.perguntas, R.drawable.respostas};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup conteiner, Bundle saveInstanceState) {

    View view = inflater.inflate(R.layout.layout_frag_1, conteiner, false);

    gridView = (GridView) view.findViewById(R.id.imageFrag1);
    gridView.setAdapter(new Adaptador(view.getContext(), listaImagens));

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(parent.getContext(), "Imagem " +listaImagens[position], Toast.LENGTH_SHORT).show();
        }
    });

    return (view);

}

}

Class Adapter:

public class Fragment1 extends Fragment {

GridView gridView;
int [] listaImagens = new int[]{R.drawable.eu, R.drawable.perguntas, R.drawable.respostas};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup conteiner, Bundle saveInstanceState) {

    View view = inflater.inflate(R.layout.layout_frag_1, conteiner, false);

    gridView = (GridView) view.findViewById(R.id.imageFrag1);
    gridView.setAdapter(new Adaptador(view.getContext(), listaImagens));

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(parent.getContext(), "Imagem " +listaImagens[position], Toast.LENGTH_SHORT).show();
        }
    });

    return (view);

}

}

  • 1

    I think you made a mistake pasting the Adapter.

1 answer

2


You can pass her position.

Example:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            Intent i = new Intent(getActivity(), Display_Image_Activity.class);
            i.putExtra("img", listaImagens[position]);
            getActivity().startActivity(i);

        }
    });

In Activity you will get the value of it, which will be for example:

String img;

Bundle extras = getIntent().getExtras();
if(extras != null) {
    img = extras.getString("img");
}

Resources res = getResources();
int resID = res.getIdentifier(img , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resID);

ImageView iv = new ImageView(this);
iv.setImageDrawable(drawable);

The variable img will have the value of R.drawable.questions, for example.

  • 1

    His answer is incomplete. I think he also wants to know how to receive this data in another Activity.

  • @seamusd I put the complement of the second Activity, thanks for the tip

  • You noticed that he says that "...take this and send to another Activity..." when in fact he is in a Fragment. As the question ta poorly formulated, you have thought of these hypothesis that it intends to send from one Fragment to another Fragment?!

  • True, had not noticed the confusion, I edited my answer, I imagine that the first case is a Fragment, and where will receive the data be an Activity

  • @seamusd I’m sorry for the poorly worded question and everyone who helped me. I figured out how to do what I wanted with Fragments.

Browser other questions tagged

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