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

我的 CameraFragment.java 文件中需要一个相机功能。我已经有了相机的代码,它在一个空的应用程序中工作(当我把它放在我的 MainActivity 中时),但我不知道将代码放在我的 CameraFragment.java 中的什么位置。


我真的是 Android Studio 的初学者,但我在互联网上找不到答案。Stack Overflow 上的新功能。


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();

        }

    }

}

}


慕容3067478
浏览 72回答 2
2回答

至尊宝的传说

让我知道这对你有用。如果您需要更多帮助进行设置,请发表评论。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@Overridepublic 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;}@Overridepublic 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();        }    }}}

白猪掌柜的

在片段中使用与活动相同但公开方法 尝试此代码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@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {&nbsp; &nbsp; view = inflater.inflate(R.layout.fragment_chat, container, false);&nbsp; &nbsp; btnCapture =(Button)view.findViewById(R.id.btnTakePicture);&nbsp; &nbsp; imgCapture = (ImageView)view.findViewById(R.id.capturedImage);&nbsp; &nbsp; btnCapture.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(cInt,Image_Capture_Code);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; return view;}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {&nbsp; &nbsp; super.onActivityResult(requestCode, resultCode, data);&nbsp; &nbsp; if (requestCode == Image_Capture_Code) {&nbsp; &nbsp; &nbsp; &nbsp; if (resultCode == RESULT_OK) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bitmap bp = (Bitmap) data.getExtras().get("data");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgCapture.setImageBitmap(bp);&nbsp; &nbsp; &nbsp; &nbsp; } else if (resultCode == RESULT_CANCELED) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java