android 如何在点击按钮时显示摄像头
How to show camera on button click in android
我有一个带有标记为相机的 TextView 的布局。单击该 TextView 我必须显示相机。布局设置为对话框。以下是我完成的代码
public class SelectCameraOrGalleryAlertDialog extends Dialog {
public SelectCameraOrGalleryAlertDialog(Context context) {
super(context);
}
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.alerttoselectcameraorgallery);
final View txtCamerabutton = (View) findViewById(R.id.txtcamerabutton);
txtCamerabutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
}
});
}
}
此处错误显示为
方法 startActivity(Intent) 未定义类型 new View.OnClickListener(){}
这是布局
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:gravity="center"
android:background="#e29567">
<TextView
android:layout_width="126dp"
android:layout_height="fill_parent"
android:drawableLeft="@drawable/addressicon"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:text="Add Photo"
android:textSize="19sp"
android:textColor="#3a3a3a"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_gravity="center"
android:orientation="horizontal"
android:background="#3a3a3a" >
<TextView
android:id="@+id/txtcamerabutton"
android:layout_width="94dp"
android:layout_height="fill_parent"
android:paddingTop="5dp"
android:drawableTop="@drawable/editicon"
android:gravity="center"
android:scaleX=".5"
android:scaleY=".5"
android:text="Camera" />
<TextView
android:id="@+id/txtgallerybutton"
android:layout_width="94dp"
android:layout_height="fill_parent"
android:paddingTop="5dp"
android:drawableTop="@drawable/replaceicon"
android:gravity="center"
android:scaleX=".5"
android:scaleY=".5"
android:text="Gallery" />
</LinearLayout>
</LinearLayout>
如何解决?
在 Button 的 onClick 中而不是 startActivity(intent)
使用 startActivityForResult(intent, 0)
,
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
并在清单文件中添加 Camers Uses Permission。
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
有关详细信息,请查看此 link。
试试下面的代码
public class SelectCameraOrGalleryAlertDialog extends Dialog {
private Context mCtx;
public SelectCameraOrGalleryAlertDialog(Context context) {
super(context);
mCtx = context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.alerttoselectcameraorgallery);
final View txtCamerabutton = (View) findViewById(R.id.txtcamerabutton);
txtCamerabutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
mCtx. startActivity(intent);
}
});
}
我有一个带有标记为相机的 TextView 的布局。单击该 TextView 我必须显示相机。布局设置为对话框。以下是我完成的代码
public class SelectCameraOrGalleryAlertDialog extends Dialog {
public SelectCameraOrGalleryAlertDialog(Context context) {
super(context);
}
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.alerttoselectcameraorgallery);
final View txtCamerabutton = (View) findViewById(R.id.txtcamerabutton);
txtCamerabutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
}
});
}
}
此处错误显示为 方法 startActivity(Intent) 未定义类型 new View.OnClickListener(){}
这是布局
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:gravity="center"
android:background="#e29567">
<TextView
android:layout_width="126dp"
android:layout_height="fill_parent"
android:drawableLeft="@drawable/addressicon"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:text="Add Photo"
android:textSize="19sp"
android:textColor="#3a3a3a"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_gravity="center"
android:orientation="horizontal"
android:background="#3a3a3a" >
<TextView
android:id="@+id/txtcamerabutton"
android:layout_width="94dp"
android:layout_height="fill_parent"
android:paddingTop="5dp"
android:drawableTop="@drawable/editicon"
android:gravity="center"
android:scaleX=".5"
android:scaleY=".5"
android:text="Camera" />
<TextView
android:id="@+id/txtgallerybutton"
android:layout_width="94dp"
android:layout_height="fill_parent"
android:paddingTop="5dp"
android:drawableTop="@drawable/replaceicon"
android:gravity="center"
android:scaleX=".5"
android:scaleY=".5"
android:text="Gallery" />
</LinearLayout>
</LinearLayout>
如何解决?
在 Button 的 onClick 中而不是 startActivity(intent)
使用 startActivityForResult(intent, 0)
,
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
并在清单文件中添加 Camers Uses Permission。
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
有关详细信息,请查看此 link。
试试下面的代码
public class SelectCameraOrGalleryAlertDialog extends Dialog {
private Context mCtx;
public SelectCameraOrGalleryAlertDialog(Context context) {
super(context);
mCtx = context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.alerttoselectcameraorgallery);
final View txtCamerabutton = (View) findViewById(R.id.txtcamerabutton);
txtCamerabutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
mCtx. startActivity(intent);
}
});
}