致命异常:java.lang.RuntimeException:takePicture 失败
Posted
技术标签:
【中文标题】致命异常:java.lang.RuntimeException:takePicture 失败【英文标题】:FATAL EXCEPTION: java.lang.RuntimeException: takePicture failed 【发布时间】:2014-04-28 09:45:18 【问题描述】:我正在尝试在不预览相机的情况下拍摄照片。我参考了很多文章,其中一些是:
http://cell0907.blogspot.in/2014/01/android-camera-capture-without.html
http://android-er.blogspot.com.es/2010/12/implement-takepicture-function-of.html
我已经添加了相机和写入外部存储的权限。
在我的活动的 xml 中,我使用了一个宽度和高度为 1dp 的surfaceView
组件。还有一个按钮,单击此按钮,必须捕获图片。下面是我的 XML 文件的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
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=".CameraActivity"
android:background="#000000" >
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="capture" />
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_
android:layout_
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="78dp" />
</RelativeLayout>
现在参考上面提到的链接,我在相应的 Activity 文件中执行了以下操作:
package com.samplecamera;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class CameraActivity extends Activity
SurfaceView sv=null;
Camera cam=null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
cam=Camera.open();
sv=(SurfaceView) findViewById(R.id.surfaceView1);
try
cam.setPreviewDisplay(sv.getHolder());
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
cam.startPreview();
Camera.Parameters params = cam.getParameters();
params.setJpegQuality(100);
cam.setParameters(params);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View arg0)
// TODO Auto-generated method stub
cam.startPreview();
cam.takePicture(null, null, mCall);
);
Camera.PictureCallback mCall = new Camera.PictureCallback()
public void onPictureTaken(byte[] data, Camera camera)
//decode the data obtained by the camera into a Bitmap
//display.setImageBitmap(photo);
// Bitmap bitmapPicture
// = BitmapFactory.decodeByteArray(data, 0, data.length);
FileOutputStream fos = null;
try
fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+
DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime())+".jpeg");
catch (FileNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
try
fos.write(data);
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
try
fos.flush();
fos.close();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
;
@Override
protected void onDestroy()
// TODO Auto-generated method stub
cam.release();
super.onDestroy();
@Override
public void onBackPressed()
// TODO Auto-generated method stub
super.onBackPressed();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.camera, menu);
return true;
点击按钮我得到以下异常:
03-21 11:55:44.404: E/AndroidRuntime(27544): FATAL EXCEPTION: main
03-21 11:55:44.404: E/AndroidRuntime(27544): java.lang.RuntimeException: takePicture failed
03-21 11:55:44.404: E/AndroidRuntime(27544): at android.hardware.Camera.native_takePicture(Native Method)
03-21 11:55:44.404: E/AndroidRuntime(27544): at android.hardware.Camera.takePicture(Camera.java:1163)
03-21 11:55:44.404: E/AndroidRuntime(27544): at android.hardware.Camera.takePicture(Camera.java:1108)
03-21 11:55:44.404: E/AndroidRuntime(27544): at com.samplecamera.CameraActivity$2.onClick(CameraActivity.java:55)
03-21 11:55:44.404: E/AndroidRuntime(27544): at android.view.View.performClick(View.java:4084)
03-21 11:55:44.404: E/AndroidRuntime(27544): at android.view.View$PerformClick.run(View.java:16987)
03-21 11:55:44.404: E/AndroidRuntime(27544): at android.os.Handler.handleCallback(Handler.java:615)
03-21 11:55:44.404: E/AndroidRuntime(27544): at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 11:55:44.404: E/AndroidRuntime(27544): at android.os.Looper.loop(Looper.java:137)
03-21 11:55:44.404: E/AndroidRuntime(27544): at android.app.ActivityThread.main(ActivityThread.java:4802)
03-21 11:55:44.404: E/AndroidRuntime(27544): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 11:55:44.404: E/AndroidRuntime(27544): at java.lang.reflect.Method.invoke(Method.java:511)
03-21 11:55:44.404: E/AndroidRuntime(27544): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
03-21 11:55:44.404: E/AndroidRuntime(27544): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
03-21 11:55:44.404: E/AndroidRuntime(27544): at dalvik.system.NativeStart.main(Native Method)
请帮忙处理一下。
【问题讨论】:
这是你的 onClick(CameraActivity.java:55) @GauravPandey:感谢您的回复。我已经把整个代码放在这里了。所以由于我只有一个按钮,所以在上面的例子中只有一个 onclick。 查看此链接***.com/questions/21723557/… 【参考方案1】:从 onclick 事件中删除 cam.startpreview() 函数,并在为媒体相关项目进行编码时尝试检查 camera==null 然后进一步移动,通过使用此您的代码不会崩溃
【讨论】:
感谢您的回复。我试过你要求我做的改变,它不起作用。以上是关于致命异常:java.lang.RuntimeException:takePicture 失败的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.RuntimeException: Parcelable 遇到 IOException 写入可序列化
Android ProgressDialog 不能与 AsyncTask 一起使用
Ionic/Cordova 应用程序找不到 MainActivity
troubshooting-sqoop 导出 TiDB表数据报com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communicatio