public class CameraActivity extends Activity {
Button camera;
ImageView imv;
final static int CAMERA_RESULT = 0;
Intent i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
camera = (Button) findViewById(R.id.button1);
imv = (ImageView) findViewById(R.id.imageView1);
camera.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
i = new Intent(
"android.provider.MediaStore.ACTION_IMAGE_CAPTURE");
startActivityForResult(i, CAMERA_RESULT);
}
});
}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
imv = (ImageView) findViewById(R.id.imageView1);
imv.setImageBitmap(bmp);
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="camera.mohit"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-feature android:name="android.hardware.camera"></uses-feature>
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".CameraActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
logcat的:
03-06 22:39:36.772: D/AndroidRuntime(282): Shutting down VM
03-06 22:39:36.772: W/dalvikvm(282): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-06 22:39:36.842: E/AndroidRuntime(282): FATAL EXCEPTION: main
03-06 22:39:36.842: E/AndroidRuntime(282): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.provider.MediaStore.ACTION_IMAGE_CAPTURE }
03-06 22:39:36.842: E/AndroidRuntime(282): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
03-06 22:39:36.842: E/AndroidRuntime(282): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
03-06 22:39:36.842: E/AndroidRuntime(282): at android.app.Activity.startActivityForResult(Activity.java:2817)
03-06 22:39:36.842: E/AndroidRuntime(282): at camera.mohit.CameraActivity$1.onClick(CameraActivity.java:33)
03-06 22:39:36.842: E/AndroidRuntime(282): at android.view.View.performClick(View.java:2408)
03-06 22:39:36.842: E/AndroidRuntime(282): at android.view.View$PerformClick.run(View.java:8816)
03-06 22:39:36.842: E/AndroidRuntime(282): at android.os.Handler.handleCallback(Handler.java:587)
03-06 22:39:36.842: E/AndroidRuntime(282): at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 22:39:36.842: E/AndroidRuntime(282): at android.os.Looper.loop(Looper.java:123)
03-06 22:39:36.842: E/AndroidRuntime(282): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-06 22:39:36.842: E/AndroidRuntime(282): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 22:39:36.842: E/AndroidRuntime(282): at java.lang.reflect.Method.invoke(Method.java:521)
03-06 22:39:36.842: E/AndroidRuntime(282): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 22:39:36.842: E/AndroidRuntime(282): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 22:39:36.842: E/AndroidRuntime(282): at dalvik.system.NativeStart.main(Native Method)
03-06 22:39:42.611: I/Process(282): Sending signal. PID: 282 SIG: 9
03-06 22:39:59.422: D/AndroidRuntime(290): Shutting down VM
03-06 22:39:59.422: W/dalvikvm(290): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-06 22:39:59.442: E/AndroidRuntime(290): FATAL EXCEPTION: main
03-06 22:39:59.442: E/AndroidRuntime(290): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.provider.MediaStore.ACTION_IMAGE_CAPTURE }
03-06 22:39:59.442: E/AndroidRuntime(290): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
03-06 22:39:59.442: E/AndroidRuntime(290): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
03-06 22:39:59.442: E/AndroidRuntime(290): at android.app.Activity.startActivityForResult(Activity.java:2817)
03-06 22:39:59.442: E/AndroidRuntime(290): at camera.mohit.CameraActivity$1.onClick(CameraActivity.java:33)
03-06 22:39:59.442: E/AndroidRuntime(290): at android.view.View.performClick(View.java:2408)
03-06 22:39:59.442: E/AndroidRuntime(290): at android.view.View$PerformClick.run(View.java:8816)
03-06 22:39:59.442: E/AndroidRuntime(290): at android.os.Handler.handleCallback(Handler.java:587)
03-06 22:39:59.442: E/AndroidRuntime(290): at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 22:39:59.442: E/AndroidRuntime(290): at android.os.Looper.loop(Looper.java:123)
03-06 22:39:59.442: E/AndroidRuntime(290): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-06 22:39:59.442: E/AndroidRuntime(290): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 22:39:59.442: E/AndroidRuntime(290): at java.lang.reflect.Method.invoke(Method.java:521)
03-06 22:39:59.442: E/AndroidRuntime(290): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 22:39:59.442: E/AndroidRuntime(290): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 22:39:59.442: E/AndroidRuntime(290): at dalvik.system.NativeStart.main(Native Method)
03-06 22:40:03.022: I/Process(290): Sending signal. PID: 290 SIG: 9
以上是我用于从相机获取图片预览的3个文件。我已经完成了这个帖子:Capture Image from Camera and Display in Activity。不知道为什么但事情变得更糟。
答案 0 :(得分:0)
你能试试吗?
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
此行正在我的手机和模拟器上运行,ACTION_IMAGE_CAPTURE值为:
public static final java.lang.String ACTION_IMAGE_CAPTURE = "android.media.action.IMAGE_CAPTURE";