我试图在单击按钮后打开相机,然后在屏幕上显示捕获的图片,但是当我单击按钮时没有任何反应。相机没有打开。下面是我的 java 程序的代码及其对应的 xml 文件.
我的课:
package com.example.splashscreen;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class CameraappActivity extends Activity implements View.OnClickListener{
ImageButton imb1;
ImageView imv1;
Button b1;
final static int cameradata=1;
Bitmap bmp;
Intent i1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cameraapp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cameraapp, menu);
initialise();
imb1.setOnClickListener(this);
b1.setOnClickListener(this);
return true;
}
private void initialise() {
// TODO Auto-generated method stub
imv1=(ImageView)findViewById(R.id.imview1);
imb1=(ImageButton)findViewById(R.id.imb2);
System.out.println("Hello");
b1=(Button)findViewById(R.id.setw);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.imb2:i1=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
System.out.println("entered");
startActivityForResult(i1, cameradata);
break;
case R.id.setw:
try{
getApplicationContext().setWallpaper(bmp);}
catch(IOException e){
e.printStackTrace();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
System.out.println("Hi1");
if(resultCode==RESULT_OK)
{
System.out.println("Hi2");
Bundle b1=data.getExtras();
bmp=(Bitmap)b1.get("data");
imv1.setImageBitmap(bmp);
}
}
}
我的 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CameraappActivity" >
<ImageButton
android:id="@+id/imb2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_above="@+id/setw"
android:layout_alignLeft="@+id/setw"
android:layout_marginBottom="24dp"
android:src="@drawable/sunset1"
android:contentDescription="Take picture"/>
<ImageView
android:id="@+id/imview1"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_above="@+id/imb2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="96dp"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/setw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:text="@string/sew" />
</RelativeLayout>