[英]How to take a picture and replace the image in android
I have a floating action button (FAB), when I press the FAB it will appear the dialog layout.我有一个浮动操作按钮 (FAB),当我按下 FAB 时,它会出现对话框布局。 how can I take a picture then replace the ImageView with picture that i take when the button text name "Shoot" clicked ?
我怎样才能拍一张照片,然后用我在点击按钮文本名称“拍摄”时拍摄的照片替换 ImageView?
here my activity_store.xml for FAB :这里是我的 FAB 的 activity_store.xml :
<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这里我的 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 :这是我的 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.抱歉英语不好。
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并从onActivityResult获取数据并将其设置为 imageView
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然后在您的活动或片段中使用相机意图加载相机 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然后在您的 onActivityResult 处理数据并设置为您的图像视图
@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您可以在此处找到更多信息https://developer.android.com/training/camera/photobasics我希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.