如何从Android中的片段启动相机

时间:2019-01-20 10:37:15

标签: java android android-fragments

我的CameraFragment.java文件中需要一个摄像头功能。我已经有了相机的代码,并且它可以在一个空的应用程序中工作(当我将其放在MainActivity中时),但是我不知道将代码放在我的CameraFragment.java中的位置。

我确实是Android Studio的初学者,但是我无法在互联网上找到答案。堆栈溢出的新功能。

CameraFragment.java

public class CameraFragment extends Fragment{

public static final String EXTRA_INFO = "default";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_camera, container, false);
}
}

并且我的CameraFragment文件中需要此代码:

public class MainActivity extends AppCompatActivity {

private Button btnCapture;
private ImageView imgCapture;
private static final int Image_Capture_Code = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_camera);
    btnCapture =(Button)findViewById(R.id.btnTakePicture);
    imgCapture = (ImageView) findViewById(R.id.capturedImage);
    btnCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cInt,Image_Capture_Code);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
    data) {
    if (requestCode == Image_Capture_Code) {
        if (resultCode == RESULT_OK) {
            Bitmap bp = (Bitmap) data.getExtras().get("data");
            imgCapture.setImageBitmap(bp);
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

让我知道它如何为您工作。如果您需要更多帮助进行设置,请发表评论。

public class CameraFragment extends Fragment {
public static final String EXTRA_INFO = "default";
private Button btnCapture;
private ImageView imgCapture;
private static final int Image_Capture_Code = 1;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_camera, container, false);
    btnCapture =(Button) view.findViewById(R.id.btnTakePicture);
    imgCapture = (ImageView) view.findViewById(R.id.capturedImage);
    btnCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cInt,Image_Capture_Code);
        }
    });

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Image_Capture_Code) {
        if (resultCode == RESULT_OK) {
            Bitmap bp = (Bitmap) data.getExtras().get("data");
            imgCapture.setImageBitmap(bp);
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
        }
    }
}}

答案 1 :(得分:0)

在片段中与活动使用相同,但将方法公开 尝试此代码

public class ChatFragment extends Fragment {
private RecyclerView chatRecylerview;
View view;
ChatUserlistAdapter userlistAdapter;
LinearLayoutManager manager;
ArrayList<HashMap<String, String>> userDetail = new ArrayList<>();
HashMap<String, String> data;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_chat, container, false);

    btnCapture =(Button)view.findViewById(R.id.btnTakePicture);
    imgCapture = (ImageView)view.findViewById(R.id.capturedImage);
    btnCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cInt,Image_Capture_Code);
        }
    });
    return view;
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Image_Capture_Code) {
        if (resultCode == RESULT_OK) {
            Bitmap bp = (Bitmap) data.getExtras().get("data");
            imgCapture.setImageBitmap(bp);
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        }
    }
}

}