1

I am trying to make an imageview of the captured image in every fragment, but it seems that when capturing an image, the preview shows in the first fragment every time, here is my code:

      public static class TestFragment extends Fragment {
    int mNum;
    String s;

    /**
     * * Create a new instance of CountingFragment, providing "num" * as an
     * argument.
     */

    public static TestFragment newInstance(int position) {
        // Log.i("Pager", "TestFragment.newInstance()");

        TestFragment fragment = new TestFragment();

        Bundle args = new Bundle();
        args.putInt("position", position);
        args.putString("s", array[position]);
        fragment.setArguments(args);

        return fragment;

    }

    /** * When creating, retrieve this instance's number from its arguments. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("position")
                : 1;
        s=getArguments() != null ? getArguments().getString("s")
                : "";
    }

    /**
     * * The Fragment's UI is just a simple text view showing its instance
     * number.
     */

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View layout = inflater.inflate(R.layout.add_read_meter, null);
        int position = getArguments().getInt("position");

        TextView tv = (TextView) layout.findViewById(R.id.clientNameId);
        tv.setText("Fragment #" + mNum+"  :"+s);

        // iv=(ImageView)layout.findViewById(R.id.imagev);
        Button b2 = (Button) layout.findViewById(R.id.imagBtnId);
        b2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Uri _fileUri = getOutputMediaFile();

                myUri = _fileUri;
                // Log.i(TAG, myUri.toString());
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, _fileUri);
                getActivity().startActivityForResult(intent,
                        CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

            }

        });
        return layout;

    }

and here is the onActivityResult method :

     @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE
            && resultCode == Activity.RESULT_OK) {
         TestFragment myFragment = (TestFragment)
         getSupportFragmentManager().findFragmentById(R.id.pager);
         myFragment.getId();
                    Bitmap bitmap;
        if (myUri != null) {
            try {

                bitmap = MediaStore.Images.Media.getBitmap(
                        this.getContentResolver(), myUri);
                // Bitmap photo = (Bitmap) data.getExtras().get("data");
                ImageView iv = (ImageView) findViewById(R.id.imagev);

                iv.setImageBitmap(bitmap);

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(FragmentPagerSupport.this, myUri.toString(),
                Toast.LENGTH_LONG).show();
    } else if (resultCode == RESULT_CANCELED) {
        // User cancelled the image capture
    } else {
        // Image capture failed, advise user
    }

}
antonyt
  • 21,863
  • 9
  • 71
  • 70
Rana Alhasan
  • 15
  • 1
  • 5

0 Answers0