Solution 1 :
first off all you have to use camera permission in you manifest
<uses-permission android_name="android.permission.CAMERA" />
<uses-feature android_name="android.hardware.camera"
android_required="true" />
then in your activity or fragment use camera intent to load camera api
int REQUEST_IMAGE_CAPTURE=2001
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
then in your onActivityResult handle data and set to your image view
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
you can find more information here https://developer.android.com/training/camera/photobasics
i hope was helpfull
Solution 2 :
to open camera
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, PICK_FROM_CAMERA);
add permission
<uses-permission android_name="android.permission.CAMERA" />
and take the data from onActivityResult and set it to imageView
Problem :
I have a floating action button (FAB), when I press the FAB it will appear the dialog layout. how can I take a picture then replace the ImageView with picture that i take when the button text name “Shoot” clicked ?
here my activity_store.xml for FAB :
<RelativeLayout
android_layout_width="match_parent"
android_layout_height="91dp"
android_layout_below="@+id/listviewLayout">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android_id="@+id/fabAdd"
android_layout_width="77dp"
android_layout_height="91dp"
android_layout_alignParentRight="true"
android_backgroundTint="@color/white"
android_src="@drawable/ic_plus"
app_elevation="6dp"
app_fabSize="normal"
android_layout_marginRight="20dp"
android_onClick="addStock"/>
</RelativeLayout>
here my dialog_add_storestock.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
android_orientation="vertical"
android_layout_width="match_parent"
android_layout_height="match_parent">
<LinearLayout
android_id="@+id/addstockLayout"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_orientation="vertical"
android_layout_marginLeft="10dp"
android_layout_marginRight="10dp"
android_layout_marginBottom="20dp"
android_layout_marginTop="10dp">
<androidx.appcompat.widget.AppCompatButton
android_id="@+id/buttonTakePicture"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Shoot"
android_onClick="TakePicture"
/>
<ImageView
android_id="@+id/resultImage"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_src="@drawable/ic_launcher_background"
/>
</LinearLayout>
</RelativeLayout>
here my StoreActivity.java :
public class StoreActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
public void addStock(View view){
LayoutInflater inflater = this.getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.dialog_add_storestock, null);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Add Data");
alert.setView(alertLayout);
alert.setCancelable(false);
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//
}
});
alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "Data Added!", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = alert.create();
dialog.show();
}
Sorry for bad english.
Comments
Comment posted by pohon gems
where i can put the Open Camera code? inside public void addStock ?
Comment posted by Muhamed El-Banna
@pohongems if you want to open it from alertDialog .. so put openCamera in Positive button of alertDialog