2013-07-24 35 views
1

我完全瞭解Android的新聞,不幸的是,我很少有時間以正確的方式學習它,我有一個工作要發佈。 問題是:我需要拍攝一張照片,並使用我製作的算法處理她。 我做到了通過,我能找到的最簡單的方法,我知道它看起來像真的trahsie對於那些誰真正的Android(對不起)如何處理意圖拍攝的照片?

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    takePic(); 

protected void takePic(){ 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(takePictureIntent, 100); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Bundle extras = data.getExtras(); 
    mImageBitmap = (Bitmap) extras.get("data"); 
      Algorithm(mImageBitmap) 

不過,這並不過程,它需要一張照片,問保存或cancell和葉中的應用,我已經通過不同的方式作出(創建一個新的活動),但似乎沒有任何工作

+0

你的算法(..)方法做什麼?我會假設這是崩潰 – dymmeh

+0

這可能有助於 - http://stackoverflow.com/questions/15248265/camera-intent-not-working-with-samsung-galaxy-s3/15287164#15287164 – Carbon

回答

0

繼承人我是如何做到的

要轉到相機:

在某處,聲明一個fileUri變量並保持在它

Uri fileUri; 
final int TAKE_PICTURE=100;//this can be any int, really 
public void goToCamera(){ 
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
    File photo; 
    try 
    { 
     // place where to store camera taken picture 
     photo = this.createTemporaryFile("picture", ".jpg"); 
     Log.v(TAG, "Here(after createTempFile)"); 
     photo.delete(); 
    } 
    catch(Exception e) 
    { 
     Log.v(TAG, "Can't create file to take picture!" + e.getMessage()); 
     Toast.makeText(context, "Please check SD card!", Toast.LENGTH_SHORT).show(); 
     return; 
    } 
    fileUri = Uri.fromFile(photo); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
    //Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, TAKE_PICTURE); 
} 

然後中檢索圖像

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

    if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK){ 
     this.getContentResolver().notifyChange(uri, null); 
     ContentResolver cr = this.getContentResolver(); 
     Bitmap bitmap; 
     try 
     { 
      BitmapFactory.Options ops = new BitmapFactory.Options(); 
      ops.inSampleSize = 4; 
      bitmap = BitmapFactory.decodeFile(uri.getPath().toString(), ops); 
     } 
     catch (Exception e) 
     { 
      Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show(); 
      Log.d(TAG, "Failed to load", e); 
     } 
    } 
} 

上面提到的創建臨時文件:

private File createTemporaryFile(String part, String ext) throws Exception 
{ 
    File tempDir= Environment.getExternalStorageDirectory(); 
    tempDir=new File(tempDir.getAbsolutePath()+"/.temp/"); 
    Log.i(TAG, tempDir.toString()); 
    if(!tempDir.exists()) 
    { 
     Log.i(TAG, "Dir doesnt exist"); 
     tempDir.mkdirs(); 
    } 
    return File.createTempFile(part, ext, tempDir); 
} 

我知道這是不是可能是因爲你所期望的那樣簡單,但是這種方法似乎儘可能靈活和兼容。讓我知道,如果我離開了其他任何東西