Fragmentpageradapter片段中的相机

时间:2015-07-25 14:06:32

标签: android android-fragments android-camera-intent

我正在尝试使用FragmentPagerAdapter实现Fragments。在我尝试使用intent实现Camera的一个片段中..但是一旦选中该选项卡,应用程序就会关闭。任何人都可以建议我一个简单的方法实现相机。 代码MyFrag2.java:

    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.Toast;

    import java.io.File;
    import java.io.IOException;
    public class MyFrag2 extends Fragment {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;
ImageView img;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    img = (ImageView)getView().findViewById(R.id.imgview);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,0);
        }
    });
    //

}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bp = (Bitmap) data.getExtras().get("data");
    img.setImageBitmap(bp);

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      return inflater.inflate(R.layout.my_frag_layout2, container, false);
}
    }

1 个答案:

答案 0 :(得分:0)

在片段中,我们必须在oncreateview中读取imageview

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle    savedInstanceState) {

View view = inflater.inflate(R.layout.my_frag_layout2, container);

img = (ImageView)view.findViewById(R.id.imgview);
img.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,0);
    }
});

  return view;
}