我创建了一个自定义相机应用程序,用于捕获图像并将图像存储在图库中。我的应用程序工作得很好,但我对视图有小问题。
目前我的应用程序看起来像这样
当我在FrameLayout中使用Button并将其设置为Foreground时,我得到了一个Crash。我已在下面发布了编码
我的MainActivity.class
public class MainActivity extends Activity {
private ImageHandler preview;
Camera camera;
int cameraID, currentCamera;
Button capture;
FrameLayout imageLayout;
SurfaceView imgl;
private static final String TAG = "CallCamera";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* Invoking Camera and Setting the ImageHandler for preview
*/
camera = getCameraInstance();
preview = new ImageHandler(this, camera);
/*
* Getting the ID of the FrameLayout that displays the camera
*/
imageLayout = (FrameLayout) findViewById(R.id.preview_layout);
imageLayout.addView(preview);
/*
* Getting the ID of capture Button and setting its Functionality
*/
capture = (Button) findViewById(R.id.cameraButton);
capture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(null, null, null, pic);
}
});
}
private Camera getCameraInstance() {
// TODO Auto-generated method stub
Camera cam = null;
try {
cam = Camera.open();
} catch (Exception e) {
}
return cam;
}
PictureCallback pic = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d(TAG, "Can't create directory to save image.");
Toast.makeText(getApplicationContext(),
"Can't create directory to save image.",
Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator
+ photoFile;
File pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(getApplicationContext(),
"New Image saved:" + photoFile, Toast.LENGTH_LONG)
.show();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
} catch (Exception error) {
Log.d(TAG,
"File" + filename + "not saved: " + error.getMessage());
Toast.makeText(getApplicationContext(),
"Image could not be saved.", Toast.LENGTH_LONG).show();
}
camera.startPreview();
}
private File getDir() {
File sdDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return new File(sdDir, "MYCAMERA");
}
};
@Override
public void onResume() {
super.onResume();
if (camera == null) {
camera = Camera.open();
preview.setCamera(camera);
}
}
}
我的ImageHandler.class(预览)
public class ImageHandler extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Camera imgCamera;
List<Size> imgSupportedPreviewSizes;
public ImageHandler(Context context, Camera camera) {
super(context);
// TODO Auto-generated constructor stub
this.imgCamera = camera;
this.holder = this.getHolder();
this.holder.addCallback(this);
this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
try {
imgCamera.setPreviewDisplay(holder);
imgCamera.startPreview();
} catch (IOException e) {
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
imgCamera.setPreviewDisplay(holder);
imgCamera.startPreview();
imgCamera.setDisplayOrientation(90);
} catch (Exception e) {
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
imgCamera.stopPreview();
imgCamera.release();
}
public void setCamera(Camera camera) {
imgCamera = camera;
if (imgCamera != null) {
imgSupportedPreviewSizes = imgCamera.getParameters()
.getSupportedPreviewSizes();
requestLayout();
}
}
}
我的工作活动_main.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=".MainActivity" >
<FrameLayout
android:id = "@+id/btn_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp">
<Button
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="Capture" />
</FrameLayout>
<FrameLayout
android:id="@+id/preview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@+id/btn_layout"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" >
</FrameLayout>
</RelativeLayout>
我的更改/编辑的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=".MainActivity" >
<FrameLayout
android:id="@+id/preview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:foreground="@+id/cameraButton" >
<Button
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:text="Capture" />
</FrameLayout>
</RelativeLayout>
我的Logcat崩溃
12-05 11:19:59.346: E/AndroidRuntime(21506): FATAL EXCEPTION: main
12-05 11:19:59.346: E/AndroidRuntime(21506): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.camera/com.example.camera.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.app.ActivityThread.access$700(ActivityThread.java:140)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.os.Looper.loop(Looper.java:137)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.app.ActivityThread.main(ActivityThread.java:4944)
12-05 11:19:59.346: E/AndroidRuntime(21506): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 11:19:59.346: E/AndroidRuntime(21506): at java.lang.reflect.Method.invoke(Method.java:511)
12-05 11:19:59.346: E/AndroidRuntime(21506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-05 11:19:59.346: E/AndroidRuntime(21506): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-05 11:19:59.346: E/AndroidRuntime(21506): at dalvik.system.NativeStart.main(Native Method)
12-05 11:19:59.346: E/AndroidRuntime(21506): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.view.LayoutInflater.createView(LayoutInflater.java:619)
12-05 11:19:59.346: E/AndroidRuntime(21506): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-05 11:19:59.346: E/AndroidRuntime(21506): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:317)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.app.Activity.setContentView(Activity.java:1924)
12-05 11:19:59.346: E/AndroidRuntime(21506): at com.example.camera.MainActivity.onCreate(MainActivity.java:44)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.app.Activity.performCreate(Activity.java:5188)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
12-05 11:19:59.346: E/AndroidRuntime(21506): ... 11 more
12-05 11:19:59.346: E/AndroidRuntime(21506): Caused by: java.lang.reflect.InvocationTargetException
12-05 11:19:59.346: E/AndroidRuntime(21506): at java.lang.reflect.Constructor.constructNative(Native Method)
12-05 11:19:59.346: E/AndroidRuntime(21506): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.view.LayoutInflater.createView(LayoutInflater.java:593)
12-05 11:19:59.346: E/AndroidRuntime(21506): ... 24 more
12-05 11:19:59.346: E/AndroidRuntime(21506): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x12/d=0x0 a=3 r=0x7f080001}
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.content.res.Resources.loadDrawable(Resources.java:2239)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.widget.FrameLayout.<init>(FrameLayout.java:109)
12-05 11:19:59.346: E/AndroidRuntime(21506): at android.widget.FrameLayout.<init>(FrameLayout.java:97)
12-05 11:19:59.346: E/AndroidRuntime(21506): ... 27 more
我的编辑XML布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="Button" />
</FrameLayout>
我称之为表面视图的部分
super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main);
/*
* Invoking Camera and Setting the ImageHandler for preview
*/
camera = getCameraInstance();
preview = new ImageHandler(this, camera);
/*
* Getting the ID of the FrameLayout that displays the camera
*/
imageLayout = (FrameLayout) findViewById(R.id.preview_layout);
imageLayout.addView(preview);
/*
* Getting the ID of capture Button and setting its Functionality
*/
我的新形象
答案 0 :(得分:0)
<FrameLayout
android:id="@+id/preview_layout"
.....
android:layout_marginBottom="10dp"
android:foreground="@+id/cameraButton" > //<<<<< here
在android:foreground
属性
答案 1 :(得分:0)
android:foreground =“@ + id / cameraButton”错了。你应该将一个drawable设置为android:foreground