2012-09-28 86 views
1

我有一個拍照的功能。用戶可以從相機或畫廊拍攝照片。 如果用戶選擇一張圖片,該圖片將顯示在另一個活動中。 在其他活動中,用戶可以編輯此圖片。 在這種情況下,我想製作一個裁剪圖像的功能。 但是當它運行時,我選擇了作物,沒有任何反應。你能給我一個解決方案嗎?這是我的代碼android eclipse中的裁剪圖像

package shoop3.android.edu; 

import java.io.File; 

import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.app.AlertDialog; 

import android.content.ActivityNotFoundException; 
import android.content.ComponentName; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.ResolveInfo; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.provider.MediaStore; 
import android.view.View; 

import android.net.Uri; 

import android.os.Bundle; 
import android.os.Environment; 

import android.widget.Button; 
import android.widget.ArrayAdapter; 
import android.widget.Toast; 
import android.widget.ImageView; 

public class editorActivity extends Activity { 
    private Uri mImageCaptureUri; 
    private ImageView mImageView; 


    private static final int CROP= 1; 
    private static final int HASIL_CROP = 2; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.editor); 

     ImageView imageeditor = (ImageView)findViewById(R.id.ImageViewEditor); 
     Intent hasil = getIntent(); 

     String path = hasil.getStringExtra("pathimage"); 
     mImageCaptureUri = hasil.getData(); 
     //Bitmap b = (Bitmap) getIntent().getParcelableExtra("mImageView"); 
     imageeditor.setImageBitmap(BitmapFactory.decodeFile(path)); 

     final String [] items   = new String [] {"CROP"};    
     ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items); 
     AlertDialog.Builder builder  = new AlertDialog.Builder(this); 

     builder.setTitle("Image Editor"); 
     builder.setAdapter(adapter, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 

       if(item == 0){ 
       Intent i = getIntent(); 
       mImageCaptureUri = i.getData(); 
       startActivityForResult(i, CROP); 
       } 


       } 

     }); 

     final AlertDialog dialog = builder.create(); 

     //Button button  = (Button) findViewById(R.id.btn_crop); 
     mImageView  = (ImageView) findViewById(R.id.ImageViewEditor); 
     mImageView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialog.show(); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode != RESULT_OK) return; 

     switch (requestCode) { 


      case CROP: 
       mImageCaptureUri = data.getData(); 

       doCrop(); 

       break;   

      case HASIL_CROP:    
       Bundle extras = data.getExtras(); 

       if (extras != null) {    
        Bitmap photo = extras.getParcelable("data"); 

        mImageView.setImageBitmap(photo); 
       } 

       File f = new File(mImageCaptureUri.getPath());    

       if (f.exists()) f.delete(); 

       break; 

     } 
    } 

    private void doCrop() { 

     try{ 
      Intent cropintent = new Intent("com.android.action.CROP"); 
      cropintent.setDataAndType(mImageCaptureUri, "/image/*"); 
      cropintent.putExtra("crop", true); 
      cropintent.putExtra("aspectX", 1); 
      cropintent.putExtra("aspectY", 1); 
      cropintent.putExtra("outputX", 256); 
      cropintent.putExtra("outputY", 256); 
      cropintent.putExtra("return-data", true); 
      startActivityForResult(cropintent, HASIL_CROP); 
     }catch(ActivityNotFoundException e){ 
      String errorMessage = "Your device not Support"; 
      Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show(); 
     } 

    }} 
+0

鎖在這個http://stackoverflow.com/questions/3846338/how-to-crop-an-image-in- android – Pasha

+0

如果可能,儘量只發布相關代碼 – keyser

回答

-1

您使用的是此行中的作物的方法:

 Intent cropintent = new Intent("com.android.action.CROP"); 

並非所有Android設備的支持。我也有同樣的問題,現在我也在爲自己解決問題。

編輯 你的問題可能只是你忘了相機:

 Intent cropintent = new Intent("com.android.camera.action.CROP");