android camera fragment,How to start camera from fragment in Android

可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:

问题:

I need a camera function in my CameraFragment.java file. I already have the code for the camera and it is working in a empty application (when I put it in my MainActivity), but I don't know where to put the code in my CameraFragment.java.

I am really a beginner with Android Studio, but I couldn't find the answer on the internet. Also new on 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);

}

}

AND I NEED THIS CODE IN MY CameraFragment FILE:

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

}

}

}

}

回答1:

In fragment use same as activity but make method public try this code

public class ChatFragment extends Fragment {

private RecyclerView chatRecylerview;

View view;

ChatUserlistAdapter userlistAdapter;

LinearLayoutManager manager;

ArrayList> userDetail = new ArrayList<>();

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

}

}

}

}

回答2:

Let me know how this works for you. Comment if you need more help setting this up.

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

}

}

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值