Android Fail to connect to camera service

匿名 (未验证) 提交于 2019-12-03 09:13:36

问题:

When the user click on Camera, it open next screen after few seconds it throws Fail to connect to camera service

and it say cannot connect to camera

This is my code:

   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);           } }  

This is my layout :

      

                    

This is my AndroidManifest.xml file

     

I did this according to this link http://mobile.tutsplus.com/tutorials/android/android-sdk-quick-tip-launching-the-camera/

Please help me what is an issue.My application contain Tab ActivityGroup.

Thanks in advance..

回答1:

Here's my application code -- hope it helps:

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){                 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 

Use this camera.xml

                       

AndroidManifest.xml

 


回答2:

You forgot something in your manifest, look here Camera Class

Add this to your manifest:

 

Hope it helps!



回答3:

you have to unlock camera before getting preview display and lock camera properly before closing your app. If the camera is not locked properly, next time your app may throw exception. So that, you have to reboot your device...



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!