2013-03-28 67 views
0

爲什麼相機啓動但作物崩潰? 我沒有收到任何錯誤消息。我們遵循http://mobile.tutsplus.com/tutorials/android/capture-and-crop-an-image-with-the-device-camera/爲什麼農作物不跑?

package com.example.background; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.content.ActivityNotFoundException; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.provider.MediaStore; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.Toast; 
public class MainActivity extends Activity implements OnClickListener { 

    //keep track of camera capture intent 
    final int CAMERA_CAPTURE = 1; 
    //keep track of cropping intent 
    final int PIC_CROP = 2; 
    //captured picture 
    private Uri picUri; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //retrieve a reference to the UI button 
     Button captureBtn = (Button)findViewById(R.id.capture_btn); 
     //handle button clicks 
     captureBtn.setOnClickListener(this); 



    } 

    public void onClick(View v) { 
     if (v.getId() == R.id.capture_btn) { 
      try { 
       //use standard intent to capture an image 
       Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       //we will handle the returned data in onActivityResult 
       startActivityForResult(captureIntent, CAMERA_CAPTURE); 
      }catch(ActivityNotFoundException anfe){ 
       //display an error message 
       String errorMessage = "Whoops - your device doesn't support capturing images!"; 
       Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
       toast.show(); 
      } 

     } 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      //user is returning from capturing an image using the camera 
      if(requestCode == CAMERA_CAPTURE){ 
       picUri = data.getData(); 
       performCrop(); 
      } 
      //user is returning from cropping the image 
      else if(requestCode == PIC_CROP){ 
       //get the returned data 
       Bundle extras = data.getExtras(); 
       //get the cropped bitmap 
       Bitmap thePic = extras.getParcelable("data"); 
       //retrieve a reference to the ImageView 
       ImageView picView = (ImageView)findViewById(R.id.picture); 
       //display the returned cropped image 
       picView.setImageBitmap(thePic); 
      } 
     } 
    } 

是這裏的問題?

private void performCrop(){ 
     try { 
      //call the standard crop action intent (the user device may not support it) 
      Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
       //indicate image type and Uri 
      cropIntent.setDataAndType(picUri, "image/*"); 
       //set crop properties 
      cropIntent.putExtra("crop", "true"); 
       //indicate aspect of desired crop 
      cropIntent.putExtra("aspectX", 1); 
      cropIntent.putExtra("aspectY", 1); 
       //indicate output X and Y 
      cropIntent.putExtra("outputX", 256); 
      cropIntent.putExtra("outputY", 256); 
       //retrieve data on return 
      cropIntent.putExtra("return-data", true); 
       //start the activity - we handle returning in onActivityResult 
      startActivityForResult(cropIntent, PIC_CROP); 
     } 
     catch(ActivityNotFoundException anfe){ 
      //display an error message 
      String errorMessage = "Whoops - your device doesn't support the crop action!"; 
      Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    } 

回答

0

您的picUri變空。這就是爲什麼它裁剪捕獲的圖像時發生錯誤,所以,請參閱此link裁剪圖像。它爲我工作, 希望這會爲你工作。

0

我不知道這是你正在看到的事實,但不幸的是,這個意圖轉到AOSP相機應用程序。並非所有設備都有,更糟的是,它不再是棒棒糖了。

目前,我試圖找出是否有來自谷歌新的圖片編輯器應用程序等同或者如果我真的要開始使用該外部庫...

可惜不是有這樣的標準功能已經可用了。