2012-11-26 57 views
2

我下面這個教程:http://mobile.tutsplus.com/tutorials/android/capture-and-crop-an-image-with-the-device-camera/拍攝的圖像和種植它

我試圖創建一個具有「take pictureButton一個簡單的活動,以及ImageView,並簡單地拍攝照片,然後打開Android內置的裁剪活動。我可以毫無意外地打開相機,但拍攝照片後,代碼不會將照片發送到裁剪活動。

當裁剪活動被調用時它似乎崩潰。我不確定爲什麼會發生這種情況。我完全遵循了這個例子(除了我不需要的開始的XML東西外),並且我查看了代碼,所有東西似乎都有意義。我確定這是一個小錯誤,導致這種情況。這裏是我的活動代碼:

package com.example.project; 

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

public class ImageChoose extends Activity implements OnClickListener { 

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

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_image_choose); 
    Button takePicture = (Button)findViewById(R.id.takePicture); 
    takePicture.setOnClickListener(this); 
} 

public void onClick(View v) { 
    if (v.getId() == R.id.takePicture){ 
     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 = "Your device doesn't support photos!"; 
      Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    } 

} 

protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    if (resultCode == RESULT_OK){ 
     if (requestCode == CAMERA_CAPTURE){ 
      picUri = data.getData(); 
      performCrop(); 
     }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){ 
     String errorMessage = "Your device doesn't support photo cropping!"; 
     Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
    } 

    } 

回答

7

我已經使用這個類型action.Here的是我的代碼與下面的鏈接: - Detail Description

我希望這將幫助你。我建議你以下幾行,你應該把你的重點: -

Intent camera=new Intent(); 
camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE); 
camera.putExtra("crop", "true"); 
0

裁剪活動是股票Android相機應用程序的一部分。它可能會或可能不會在您的設備上可用,特別是如果您使用自定義/供應商相機應用程序。如果你希望它能夠可靠地工作,你必須把裁剪的代碼併入你自己的應用程序中。

-1
private void performCrop(){ 
} 
Inside this method we are going to call an Intent to perform the crop, so let’s add 「try」 and 「catch」 blocks in case the user device does not support the crop operation: 

try { 
} 
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(); 
} 

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

//keep track of cropping intent 
final int PIC_CROP = 2;