2011-10-31 2 views
0

Je nouveau dans le développement android. Je veux développer une application simple qui sera en mesure de prendre une photo en utilisant le téléphone portable camara et le montrer sur l'écran du téléphone portable.Comment puis-je prendre une photo qui a été prise?

Y at-il un exemple simple que je peux utiliser? ou un code qui peut m'aider à apprendre comment le faire?

Merci pour toute aide

+3

double possible (http://stackoverflow.com/questions/5991319/capture-image-from-camera-and-display-in-activity) [Capture d'image de la caméra et Affichage en activité] –

Répondre

2

à la caméra commencer à vous utiliser

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(cameraIntent, 0); 

et ici vous avez le handeling

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == 0) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      imageView.setImageBitmap(photo); 
     } 
    } 
0

Try this .. Utilisez le code ci-dessous dans onCreate

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
URI mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), 
     "pic_" + String.valueOf(System.currentTimeMillis()) + ".jpg")); 
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri); 

    try { 
     intent.putExtra("return-data", true); 
     startActivityForResult(intent, CAMERA_RESULT); 
    } catch (ActivityNotFoundException e) { 
     e.printStackTrace(); 
    } 

Th en onActivityResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) { 
      //Here you will get path of image stored in sdcard then pass it to next activity as your desires.. 
          mImagePath = extras.getString("image-path"); 
       mSaveUri = getImageUri(mImagePath); 
       Bitmap mBitmap = getBitmap(mImagePath); 
         // here mBitmap is assigned to any imageview and you can use it in for display 
     } 
     } 

    private Uri getImageUri(String path) { 
    return Uri.fromFile(new File(path)); 
     } 

    private Bitmap getBitmap(String path) { 
    Uri uri = getImageUri(path); 
    InputStream in = null; 
    try { 
     in = mContentResolver.openInputStream(uri); 
    return BitmapFactory.decodeStream(in).copy(Config.ARGB_8888, true); 
} catch (FileNotFoundException e) { 
    //Log.e(TAG, "file " + path + " not found"); 
} 
return null; 
     } 

      } 
Questions connexes