2012-04-11 18 views
11

से कनेक्ट करते हैं कैमरा पर उपयोगकर्ता क्लिक करें, यह अगले स्क्रीन खोलने के कुछ सेकंड के बाद फेंक देता है Fail to connect to camera serviceएंड्रॉयड विफल कैमरा सेवा

और यह कहना cannot connect to camera

को यह मेरा कोड है:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    View viewLoad = LayoutInflater.from(CameraActivity.this).inflate(R.layout.camera, null); 
    setContentView(viewLoad); 

    Bundle bundle = this.getIntent().getExtras(); 
    seletctedRetailer = bundle.getString("RetailerName"); 
    retailerCode = bundle.getString("RetailerCode"); 

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    Bundle b = new Bundle(); 
    b.putString("Activity", "RetailerOrderSActivity"); 
    b.putString("RetailerName", seletctedRetailer); 
    b.putString("RetailerCode", retailerCode); 

    cameraIntent.putExtras(b); 
    getParent().getParent().setTitle("Image Capture"); 
    startActivityForResult(cameraIntent,CAMERA_PIC_REQUEST); 

    vale +=1; 

    Button imgMCancel =(Button)findViewById(R.id.imgMCancel); 
    imgMCancel.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(getBaseContext(), CameraMainActivity.class); 
      Bundle bundle = new Bundle(); 
      bundle.putString("Activity", "CameraMainActivity"); 
      bundle.putString("RetailerName", seletctedRetailer); 
      bundle.putString("RetailerCode", retailerCode); 
      i.putExtras(bundle); 
      View vi = SalesActivityGroup.group.getLocalActivityManager().startActivity("CameraMainActivity", i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
      SalesActivityGroup.group.replaceView(vi); 
     } 
    }); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == CAMERA_PIC_REQUEST) { 
      System.out.println("====YES======"); 
      Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
       ImageView image = (ImageView) findViewById(R.id.imageView1); 
       image.setImageBitmap(thumbnail); 
     } 
} 

 <?xml version="1.0" encoding="utf-8"?> 

:

यह मेरा लेआउट है

<LinearLayout android:id="@+id/header" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="10dp" 
       android:layout_marginTop="75dp" 
       android:layout_width="fill_parent"> 


     <Button android:layout_width="wrap_content" 
       android:text="Cancel" 
       android:textColor="#FFFFFF" 
       android:background="@drawable/btn_red" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="150dp" 
       android:id="@+id/imgMCancel" 
       > 
     </Button> 

     <Button android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/buttonClick" 
       android:text="Make Photo" 
       android:textColor="#FFFFFF" 
       android:background="@drawable/btn_red" 
       android:layout_gravity="center"> 
     </Button> 


    </LinearLayout> 

यह मेरा AndroidManifest.xml फ़ाइल

 <activity android:name=".sales.CameraActivity"    android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden" android:clearTaskOnLaunch="false"> 
     <intent-filter> 
      <action android:name="android.media.action.IMAGE_CAPTURE" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

है मैं इस http://mobile.tutsplus.com/tutorials/android/android-sdk-quick-tip-launching-the-camera/

कृपया मेरी मदद की थी इस लिंक के अनुसार क्या एक issue.My आवेदन शामिल है टैब गतिविधि समूह।

अग्रिम धन्यवाद ..

+0

मैंने बस डिवाइस को पुनरारंभ करना समाप्त कर दिया और उसके बाद सबकुछ काम किया। – jwBurnside

उत्तर

6

यहाँ अपने आवेदन कोड है - आशा है कि यह मदद करता है:

public class CameraActivity extends Activity { 
    ImageView img_logo; 
    public static Bitmap bitmap; 
    public static String selectedImagePath=""; 
    protected boolean _taken; 
    protected static final String PHOTO_TAKEN = "photo_taken"; 
    int count = 0; 
    static String image1 = ""; 
    /*static String _path=""; 
    Bitmap bita; 
    ParsingForCabDetails obj_ParsingForCabDetails; 
    InputStream is; 
    protected boolean _taken; 
    protected static final String PHOTO_TAKEN = "photo_taken";*/ 
    ProgressDialog pd; 
    //ImageView iv; 
    Button cancel,Ok; 
    String alert=""; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.camera); 

     img_logo = (ImageView) findViewById(R.id.camera_image); 
     cancel = (Button) findViewById(R.id.camera_taken); 
     upload = (Button) findViewById(R.id.camerabtnupload); 

     if(image1.length()<= 0) 
     startCameraActivity(); 

     upload.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       finish(); 
       if(image1.length() > 0){ 
       pd = ProgressDialog.show(SmallyTaxiTabbar.tabContext, "", "Loading...."); 
       new Thread(new Runnable() { 

         public void run() { 

          LoardServer(); 
          handler.sendEmptyMessage(0); 

         } 
        }).start(); 

      } 
       else 
        Toast.makeText(getApplicationContext(),"No Capture Image",2000).show(); 
      } 


     }); 
     img_logo.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startCameraActivity(); 
      } 
     }); 

     cancel.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       finish(); 
       selectedImagePath= ""; 
       image1=""; 
       } 
     }); 


    } 


    public void LoardServer() { 
    // this is use for upload image or save image 

    } 


    private Handler handler = new Handler(){ 
     public void handleMessage(android.os.Message msg) { 
      pd.dismiss(); 
      finish(); 
      if(alert.length() >0){ 
       Log.d("hello","alert"+alert); 
       Toast.makeText(getApplicationContext(),alert, 2000).show(); 
      } 
      else 
       FindCabDetails.iv_camera.setImageBitmap(bitmap); 

      selectedImagePath= ""; 
      image1=""; 

     }; 
    }; 
    protected void startCameraActivity() { 
     selectedImagePath = Environment.getExternalStorageDirectory() + "/default"+count+".jpg"; 
     count++; 
     File file = new File(selectedImagePath); 
     Uri outputFileUri = Uri.fromFile(file); 
     Intent intent = new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
     startActivityForResult(intent, 0); 
     } 
     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch (resultCode) { 
     case -1: 
     onPhotoTaken(); 
     break; 
     } 
     } 
     protected void onPhotoTaken() { 
     _taken = true; 
     bitmap = BitmapFactory.decodeFile(selectedImagePath); 
     bitmap = decodeFile(); 
     image1 = selectedImagePath; 
     img_logo.setImageBitmap(bitmap); 

     } 
     @Override 
     protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     if (savedInstanceState.getBoolean(PHOTO_TAKEN)) { 
     onPhotoTaken(); 
     } 
     } 
     @Override 
     protected void onSaveInstanceState(Bundle outState) { 
     outState.putBoolean(PHOTO_TAKEN, _taken); 
     } 
     private Bitmap decodeFile() { 
     try { 
     // decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(selectedImagePath), null, o); 
     final int REQUIRED_SIZE = 70; 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
     if (width_tmp/2 < REQUIRED_SIZE 
     || height_tmp/2 < REQUIRED_SIZE) 
     break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale++; 
     } 
     // decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(new FileInputStream(selectedImagePath), null, o2); 
     } catch (FileNotFoundException e) { 
     } 
     return null; 
     } 
     public boolean onKeyDown(int keyCode, KeyEvent event) { 
      if(keyCode==KeyEvent.KEYCODE_BACK) 
      { 
       finish(); 
      } 
      return super.onKeyDown(keyCode, event); 
     } 

} 

इस camera.xml उपयोग

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:background="#FFFFFF" android:layout_height="fill_parent" android:layout_width="fill_parent"> 
     <RelativeLayout android:id="@+id/relativeLayout1" 
      android:layout_width="fill_parent" 
      android:background="@drawable/bottoombg" android:layout_height="wrap_content"> 
      <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" 
       android:layout_centerHorizontal="true" android:textColor="#FBB917" 
       android:textStyle="bold" android:textSize="20dip" android:id="@+id/txtCabNametitle" android:text="Camera"></TextView> 
      <Button android:layout_alignParentRight="true" android:layout_centerVertical="true" 
       android:background="@drawable/filter_btn1" 
       android:layout_marginRight="5dip" 
       android:layout_height="30dip" android:textColor="#FFFFFF" android:text="Upload" android:layout_width="70dip" android:id="@+id/camerabtnupload"></Button> 
      <Button android:layout_centerVertical="true" android:background="@drawable/btn_press_effect" android:layout_height="30dip" android:textColor="#FFFFFF" android:paddingLeft="10dip" android:id="@+id/camera_taken" android:text="Cancel" android:layout_width="70dip"></Button> 
     </RelativeLayout> 
     <ImageView android:id="@+id/camera_image" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:layout_height="200dip" android:layout_width="200dip"></ImageView> 
    </RelativeLayout> 

AndroidManifest.xml

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

आप अपने मेनिफ़ेस्ट में कुछ भूल गया, इधर देखो Camera Class

अपने प्रकट करने के लिए इस जोड़ें:

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

आशा है कि यह मदद करता है!

+0

मैंने अनुमति भी दी। – Piraba

+4

कृपया इस जानकारी को अपने प्रश्न में जोड़ें! आपके मैनिफेस्ट में अनुमति गुम है! – sandkasten

6

आप कैमरे अनलॉक करने के लिए है बंद होने से पहले पूर्वावलोकन डिस्प्ले और कैमरे को ठीक से लॉक करने से पहले अपने ऐप में आईएनजी अगर कैमरा ठीक से लॉक नहीं है, अगली बार जब आपका ऐप अपवाद फेंक सकता है। इसलिए, आपको अपने डिवाइस को रीबूट करना होगा ...

+0

मुझे समझ में नहीं आता कि इसका मतलब क्या है # ठीक से लॉक # # – Hugo

+0

mCamera = Camera.open(); mCamera.unlock(); और अंत में mCamera.lock(); –

+0

एपीआई स्तर 14 के बाद से, कैमरा स्वचालित रूप से प्रारंभ() में अनुप्रयोगों के लिए बंद कर दिया जाता है। – Hugo

संबंधित मुद्दे