相机应用无法正常工作

时间:2012-09-05 06:44:21

标签: android android-camera

我的相机应用程序无法正常工作。我尝试了一些现有的教程示例。但他们清楚地给出了编写Android相机应用的代码。我使用了该代码,并且总是得到以下图像错误。

mycode的:

public class MainActivity extends Activity {
    private static int  PICK_FROM_CAMERA =1;
    ImageView iv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv=(ImageView)findViewById(R.id.imageView);

    Button button=(Button)findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View view)
        {
             Intent intent = new Intent();
             intent.putExtra("aspectX", 730);
             intent.putExtra("aspectY", 1115);
             intent.putExtra("outputX", 730);
             intent.putExtra("outputY", 1115);
             intent.setAction("android.media.action.IMAGE_CAPTURE");
        startActivityForResult(intent, PICK_FROM_CAMERA);       
    }
         public void onActivityResult(int requestCode, int resultCode, Intent data) 
            {


            // switch(PICK_FROM_CAMERA)
               // {
             //   case PICK_FROM_CAMERA : if (resultCode == RESULT_OK)
              //  { 

             if(requestCode==PICK_FROM_CAMERA&&resultCode==RESULT_OK){
                    ContentValues values = new ContentValues(); 
                    values.put(Images.Media.TITLE, "title");
                    values.put(Images.Media.BUCKET_ID, "test");
                    values.put(Images.Media.DESCRIPTION, "test Image taken");
                    values.put(Images.Media.MIME_TYPE, "image/jpeg");
                    Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
                    Bitmap photo = (Bitmap) data.getExtras().get("data");

                    //((ImageView)findViewById(R.id.selectedimage)).setImageBitmap(photo);

                    Bitmap theImage=(Bitmap)data.getExtras().get("data");
                    iv.setImageBitmap(theImage);
                    OutputStream outstream;
                    try {
                        outstream = getContentResolver().openOutputStream(uri);
                        photo.compress(Bitmap.CompressFormat.JPEG,100, outstream);
                        outstream.close();
                } catch (FileNotFoundException e) {}
                catch (IOException e){}
            }
           // break;
                }


    });
    }

logcat的:

09-05 12:23:49.307: E/ActivityManager(58): ANR in com.android.camera (com.android.camera/.Camera)
09-05 12:23:49.307: E/ActivityManager(58): Reason: keyDispatchingTimedOut
09-05 12:23:49.307: E/ActivityManager(58): Load: 2.54 / 1.24 / 0.48
09-05 12:23:49.307: E/ActivityManager(58): CPU usage from 39468ms to 30ms ago:
09-05 12:23:49.307: E/ActivityManager(58):   qemud: 54% = 10% user + 43% kernel
09-05 12:23:49.307: E/ActivityManager(58):   system_server: 28% = 24% user + 4% kernel / faults: 1253 minor
09-05 12:23:49.307: E/ActivityManager(58):   mediaserver: 14% = 13% user + 0% kernel / faults: 238 minor 1 major
09-05 12:23:49.307: E/ActivityManager(58):   com.example.kkk: 0% = 0% user + 0% kernel / faults: 443 minor
09-05 12:23:49.307: E/ActivityManager(58):   adbd: 0% = 0% user + 0% kernel
09-05 12:23:49.307: E/ActivityManager(58):   m.android.phone: 0% = 0% user + 0% kernel / faults: 37 minor
09-05 12:23:49.307: E/ActivityManager(58):   ronsoft.openwnn: 0% = 0% user + 0% kernel / faults: 27 minor
09-05 12:23:49.307: E/ActivityManager(58):   d.process.acore: 0% = 0% user + 0% kernel / faults: 12 minor
09-05 12:23:49.307: E/ActivityManager(58):   rild: 0% = 0% user + 0% kernel
09-05 12:23:49.307: E/ActivityManager(58):   id.defcontainer: 0% = 0% user + 0% kernel / faults: 19 minor
09-05 12:23:49.307: E/ActivityManager(58):   logcat: 0% = 0% user + 0% kernel
09-05 12:23:49.307: E/ActivityManager(58):   pdflush: 0% = 0% user + 0% kernel
09-05 12:23:49.307: E/ActivityManager(58):   zygote: 0% = 0% user + 0% kernel / faults: 33 minor
09-05 12:23:49.307: E/ActivityManager(58):   ndroid.settings: 0% = 0% user + 0% kernel / faults: 27 minor
09-05 12:23:49.307: E/ActivityManager(58):   roid.alarmclock: 0% = 0% user + 0% kernel / faults: 8 minor
09-05 12:23:49.307: E/ActivityManager(58):   m.android.music: 0% = 0% user + 0% kernel / faults: 25 minor
09-05 12:23:49.307: E/ActivityManager(58):   com.svox.pico: 0% = 0% user + 0% kernel / faults: 29 minor
09-05 12:23:49.307: E/ActivityManager(58):  +.android.camera: 0% = 0% user + 0% kernel
09-05 12:23:49.307: E/ActivityManager(58): TOTAL: 99% = 49% user + 50% kernel + 0% irq + 0% softirq
09-05 12:23:49.727: D/dalvikvm(58): GC_FOR_MALLOC freed 1794 objects / 145104 bytes in 370ms
09-05 12:23:49.757: W/WindowManager(58): No window to dispatch pointer action 1
09-05 12:23:49.847: I/dalvikvm(217): threadid=3: reacting to signal 3
09-05 12:23:50.207: D/dalvikvm(58): GC_FOR_MALLOC freed 164 objects / 148312 bytes in 421ms
09-05 12:23:50.447: I/dalvikvm(162): Wrote stack traces to '/data/anr/traces.txt'
09-05 12:23:50.657: D/dalvikvm(58): GC_FOR_MALLOC freed 52 objects / 108088 bytes in 395ms
09-05 12:23:50.757: W/WindowManager(58): No window to dispatch pointer action 1
09-05 12:23:50.757: W/WindowManager(58): No window to dispatch pointer action 0
09-05 12:23:50.757: W/WindowManager(58): No window to dispatch pointer action 1
09-05 12:23:50.767: W/WindowManager(58): No window to dispatch pointer action 0
09-05 12:23:50.767: W/WindowManager(58): No window to dispatch pointer action 1
09-05 12:23:50.767: W/WindowManager(58): No window to dispatch pointer action 0
09-05 12:23:50.777: W/WindowManager(58): No window to dispatch pointer action 1
09-05 12:23:50.777: W/WindowManager(58): No window to dispatch pointer action 0
09-05 12:23:50.777: W/WindowManager(58): No window to dispatch pointer action 1
09-05 12:23:51.307: I/ARMAssembler(58): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x323c90:0x323d4c] in 580143 ns
09-05 12:23:52.127: I/dalvikvm(168): Wrote stack traces to '/data/anr/traces.txt'
09-05 12:23:52.718: I/dalvikvm(243): Wrote stack traces to '/data/anr/traces.txt'
09-05 12:23:55.188: I/dalvikvm(183): Wrote stack traces to '/data/anr/traces.txt'
09-05 12:23:55.308: I/dalvikvm(191): Wrote stack traces to '/data/anr/traces.txt'
09-05 12:23:55.398: I/dalvikvm(130): Wrote stack traces to '/data/anr/traces.txt'
09-05 12:23:55.538: I/dalvikvm(228): Wrote stack traces to '/data/anr/traces.txt'
09-05 12:23:55.608: I/dalvikvm(125): Wrote stack traces to '/data/anr/traces.txt'
09-05 12:23:55.898: I/dalvikvm(217): Wrote stack traces to '/data/anr/traces.txt'
09-05 12:23:55.980: I/dalvikvm(198): Wrote stack traces to '/data/anr/traces.txt'
09-05 12:23:58.777: W/ActivityManager(58):   Force finishing activity com.android.camera/.Camera
09-05 12:23:58.777: I/ActivityManager(58): Killing com.android.camera (pid=283): user's request
09-05 12:23:58.817: I/Process(58): Sending signal. PID: 283 SIG: 9
09-05 12:23:58.848: I/WindowManager(58): WIN DEATH: Window{43ffffb0 com.android.camera/com.android.camera.Camera paused=true}
09-05 12:23:58.848: I/WindowManager(58): WIN DEATH: Window{43fad698 SurfaceView paused=false}
09-05 12:23:58.887: I/ActivityManager(58): Process com.android.camera (pid 283) has died.
09-05 12:23:58.957: I/WindowManager(58): Setting rotation to 0, animFlags=1
09-05 12:23:58.977: E/CameraService(33): getClientFromCookie: client appears to have died
09-05 12:23:59.007: I/ActivityManager(58): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=1/1 orien=1 layout=18 uiMode=17 seq=5}
09-05 12:23:59.447: W/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 283 uid 10006
09-05 12:24:01.827: D/dalvikvm(58): GC_EXPLICIT freed 1171 objects / 270992 bytes in 138ms

3 个答案:

答案 0 :(得分:1)

更改setAction如下;松开“”

   Intent intent = new Intent();
     intent.putExtra("aspectX", 730);
     intent.putExtra("aspectY", 1115);
     intent.putExtra("outputX", 730);
     intent.putExtra("outputY", 1115);
     intent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PICK_FROM_CAMERA); 

答案 1 :(得分:0)

清单文件中需要相机权限。你有没有在清单文件中获得许可, 在Android清单文件

中添加此代码
<uses-permission android:name="android.permission.CAMERA" ></uses-permission>

答案 2 :(得分:0)

尝试在你的意图()中传递这个。

Intent intent =  new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

并把它放在你的清单中

<uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />