为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕容3067478 2022-05-12 17:20:10
我的 CameraFragment.java 文件中需要一个相机功能。我已经有了相机的代码,它在一个空的应用程序中工作(当我把它放在我的 MainActivity 中时),但我不知道将代码放在我的 CameraFragment.java 中的什么位置。我真的是 Android Studio 的初学者,但我在互联网上找不到答案。Stack Overflow 上的新功能。CameraFragment.javapublic class CameraFragment extends Fragment{public static final String EXTRA_INFO = "default";@Nullable@Overridepublic 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;@Overrideprotected 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);        }    });}@Overrideprotected 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 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

让我知道这对你有用。如果您需要更多帮助进行设置,请发表评论。


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

        }

    }

}}


查看完整回答
反对 回复 2022-05-12
?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

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


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

        }

    }

}

}


查看完整回答
反对 回复 2022-05-12
  • 2 回答
  • 0 关注
  • 71 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信