2011-12-21 9 views
5

के साथ एंड्रॉइड कैमरा इरादा कैमरा कैमरे के माध्यम से एक तस्वीर लेने के दौरान मैं एक छवि को फसल और आकार बदलने की कोशिश कर रहा हूं लेकिन समझ में नहीं आता कि मैं यहां क्या कर रहा हूं - फसल छवि डेटा भी नहीं प्राप्त कर सकता अगर फ़ाइल फोन पर सही ढंग से सहेजी गई है।क्रॉप

... 
public class Add extends Activity { 
    private String imageFilePath; 
    ... 
    protected void onCreate(Bundle savedInstanceState) { 
    ... 
     imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmp/" + String.valueOf(System.currentTimeMillis()) + ".jpg"; 
     imageFile = new File(imageFilePath); 
     imageFileUri = Uri.fromFile(imageFile); 
     Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
     intent.putExtra("crop", "true"); 
     intent.putExtra("outputX", 600); 
     intent.putExtra("outputY", 600); 
     intent.putExtra("aspectX", 1); 
     intent.putExtra("aspectY", 1); 
     intent.putExtra("scale", true); 
     intent.putExtra("return-data", true); 
     startActivityForResult(intent, 0); 
     ... 
    } 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode) { 
     case 0 : {  
     Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath); 
    } 
    } 
} 

अगर मैं intent.putExtra("crop", "true");, imageFilePath रिटर्न अशक्त का उपयोग करें, नहीं तो यह सही फ़ाइल देता है।

आपकी मदद के लिए धन्यवाद!

उत्तर

11

इस लाइन को हटाने के लिए यह प्रयास करें ::

private void doCrop() { 
    final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>(); 

    Intent intent = new Intent("com.android.camera.action.CROP"); 
    intent.setType("image/*"); 

    List<ResolveInfo> list = getPackageManager().queryIntentActivities(
      intent, 0); 

    int size = list.size(); 

    if (size == 0) { 
     Toast.makeText(this, "Can not find image crop app", 
       Toast.LENGTH_SHORT).show(); 

     return; 
    } else { 
     intent.setData(mImageCaptureUri); 

     /* 
     * intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); 
     */ 

     intent.putExtra("outputX", int_Height_crop); 
     intent.putExtra("outputY", int_Width_crop); 
     intent.putExtra("aspectX", 1); 
     intent.putExtra("aspectY", 1); 
     intent.putExtra("scale", true); 
     intent.putExtra("return-data", true); 

     if (size == 1) { 
      Intent i = new Intent(intent); 
      ResolveInfo res = list.get(0); 

      i.setComponent(new ComponentName(res.activityInfo.packageName, 
        res.activityInfo.name)); 

      startActivityForResult(i, CROP_FROM_CAMERA); 
     } else { 
      for (ResolveInfo res : list) { 
       final CropOption co = new CropOption(); 

       co.title = getPackageManager().getApplicationLabel(
         res.activityInfo.applicationInfo); 
       co.icon = getPackageManager().getApplicationIcon(
         res.activityInfo.applicationInfo); 
       co.appIntent = new Intent(intent); 

       co.appIntent 
         .setComponent(new ComponentName(
           res.activityInfo.packageName, 
           res.activityInfo.name)); 

       cropOptions.add(co); 
      } 

      CropOptionAdapter adapter = new CropOptionAdapter(
        getApplicationContext(), cropOptions); 

      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("Choose Crop App"); 
      builder.setAdapter(adapter, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int item) { 
          startActivityForResult(
            cropOptions.get(item).appIntent, 
            CROP_FROM_CAMERA); 
         } 
        }); 

      builder.setOnCancelListener(new DialogInterface.OnCancelListener() { 
       @Override 
       public void onCancel(DialogInterface dialog) { 

        if (mImageCaptureUri != null) { 
         getContentResolver().delete(mImageCaptureUri, null, 
           null); 
         mImageCaptureUri = null; 
        } 
       } 
      }); 

      AlertDialog alert = builder.create(); 

      alert.show(); 
     } 
    } 
} 

Cropoption ::

package com.any; 

import android.content.Intent; 
import android.graphics.drawable.Drawable; 

public class CropOption { 
    public CharSequence title; 
    public Drawable icon; 
    public Intent appIntent; 
} 
+0

धन्यवाद डॉनिक, इसलिए आप 'intent.putExtra (" फसल "," सत्य ") का उपयोग नहीं कर सकते;' ACTION_IMAGE_CAPTURE इरादे से? आपको पहले एक इरादे से कब्जा करने की आवश्यकता है तो फसल के लिए एक और इरादा का उपयोग करें? –

+0

फसलडैप्टर क्या है? –

+1

@BriceLechatellier यह स्थिर नहीं है, कुछ एंड्रॉइड डिवाइस में, कैमरा ऐप बस दुर्घटनाग्रस्त हो जाता है। एफवाईआई: http://stackoverflow.com/questions/13561026/taking-an-image-and-cropping-it –

0

प्रयास करें: intent.putExtra (, "निवेश-डेटा" सच);

आपको इसे पास करने की आवश्यकता नहीं है। कोड की वह पंक्ति आपको डेटा उद्देश्य से अपना बिटमैप पुनर्प्राप्त करने की अनुमति देगी। इस मामले में, आप एक यूआरआई से अपना बिटमैप प्राप्त कर रहे हैं।