1

我正在尝试对每个片段中捕获的图像进行图像视图,但似乎在捕获图像时,预览每次都显示在第一个片段中,这是我的代码:

      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;

    }

这是 onActivityResult 方法:

     @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
    }

}
4

0 回答 0