2013-02-27 86 views
7

我很難弄清楚如何讓用戶裁剪圖片。 我想給位圖變量加載位圖裁剪圖片,然後將其設置爲壁紙。但我沒有這麼做......這是我試過的。讓用戶裁剪圖像

第一版。 =按預期工作,但返回的圖像分辨率較差。將輸出更改爲更高值不會有幫助。正如我在某篇文章中看到的,不建議使用相機,因爲並非所有設備都支持此功能。

Intent intent = new Intent("com.android.camera.action.CROP"); 
String path = Images.Media.insertImage(context.getContentResolver(), loaded,null, null); 
Uri uri = Uri.parse(path);    
intent.setData(uri); 
intent.putExtra("crop", "true"); 
intent.putExtra("aspectX", 1); 
intent.putExtra("aspectY", 1); 
intent.putExtra("outputX", 300); 
intent.putExtra("outputY", 300); 
intent.putExtra("noFaceDetection", true); 
intent.putExtra("return-data", true);         
startActivityForResult(intent, 2); 

二。加載圖像選擇器,然後裁剪。我如何配置這個以直接在我的圖像上加載裁剪?就像在版本1

Intent photoPickerIntent = new Intent(MediaStore.ACTION_PICK); 
photoPickerIntent.setData(uri); 
photoPickerIntent.putExtra("crop", "true"); 
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); 
startActivityForResult(photoPickerIntent, 2); 

而且onActivity結果

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != RESULT_OK) { return; } 
    if(requestCode == 2) { 
     Bundle extras = data.getExtras(); 
     if(extras != null) { 
      Bitmap photo = extras.getParcelable("data"); 
      loaded = photo; 
     } 
     WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext()); 

     try { 
      myWallpaperManager.setBitmap(loaded); 
     } catch (IOException e) {} 
    } 
} 

我不whever知道這些都是正確的方法,使這個工作,但我希望有人可以點我在正確的方向。哪些,爲什麼以及如何使用。

更新:我仍然在等待有人指出,如何做到這一點正確,答案如下工作,但較差的分辨率返回圖像,所以他們不使用

+0

只是在用戶返回到您的活動之前添加裁剪方法。在你設置view.setImageBitmap之前或者你做的任何事情都要通過裁剪方法。 – k0sh 2013-02-27 15:49:13

+0

@Datenshi我回來了,你的解決方案使用這個鏈接https://github.com/edmodo/cropper,這是一個演示項目代碼巫婆解決你的問題。 – 2014-04-07 10:42:35

+0

@Dennshi試試這個代碼,我知道這次我沒有錯! – 2014-04-07 10:43:52

回答

-2

我也有一個選項使用相機的問題和ACTION_PICK即使傳遞的分辨率更大,返回的圖像非常小。我得到了解決這個存儲導致農作物圖像中的臨時文件

// temporary storage location, preferably in your cache directory 
private final String tempFilePath = "somePath"; 

// URI instantiated with your temporary storage location 
private Uri tempuri = Uri.fromFile(new File(tempFilePath)); 

// code for startActivityForResult 
private final static int ACTIVITY_CROP_IMAGE = 1; 

,並呼籲像這樣

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
photoPickerIntent.setType("image/*"); 
photoPickerIntent.putExtra("crop", "true"); 
photoPickerIntent.putExtra("aspectX", wallpaperWidth); 
photoPickerIntent.putExtra("aspectY", wallpaperHeight); 
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempuri); 
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); 
startActivityForResult(photoPickerIntent, ACTIVITY_CROP_IMAGE); 

的意圖,然後在onActivityResult

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

    if(requestCode == ACTIVITY_CROP_IMAGE) 
    { 
     try 
     { 
      Bitmap bmp = BitmapFactory.decodeFile(tempFilePath); 
      if(bmp != null) 
      { 
       WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext()); 
       myWallpaperManager.setBitmap(bmp); 
      } 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      if(tempFilePath != null) 
      { 
       File tempFile = new File(tempFilePath); 
       if(tempFile.exists()) 
       { 
        tempFile.delete(); 
       } 
      } 
     } 
    } 
} 

我從構建上面的代碼我的頭頂,並沒有真正編譯它。但基本是正確的,你應該得到的竅門;-)

2

首先,變量:

final int PIC_CROP = 2; 

Uri imageUri; 
Bitmap thePic; 

之前,你從你的相機或畫廊PIC,把你的圖像轉換成一個URI(imageUri) ,用在這裏稱爲 「performCrop()」 方法內部的try/catch:

private void performCrop(){ 
     try { 
      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) { 
       intent.setData(imageUri);   
       intent.putExtra("crop", "false"); 
       intent.putExtra("aspectX", 1); 
       intent.putExtra("aspectY", 1); 
       intent.putExtra("outputX", 256); 
       intent.putExtra("outputY", 256); 
       intent.putExtra("scale", true); 
       intent.putExtra("return-data", true); 

       Intent i = new Intent(intent); 
       ResolveInfo res = list.get(0); 
       i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 

       startActivityForResult(i, PIC_CROP); 
      } 

     } 
     catch(ActivityNotFoundException anfe){ 
      String errorMessage = "Whoops - your device doesn't support the crop action!"; 
      Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    } 

在方法onActivityResult:

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 

     if(requestCode == PIC_CROP){ 

     if (resultCode == RESULT_OK) { 
      Bundle extras = intent.getExtras(); 
      thePic = extras.getParcelable("data"); 
      imageViewPhoto.setImageBitmap(thePic); //in my case, set the image on screen 

     }else{ 
      //do something 
     } 
    } 
} 
+0

如何傳遞drawable作爲imageuri? – 2014-06-11 04:44:11

+0

@ShabbirDhangot在這裏檢查http://stackoverflow.com/a/19567921/1945754 – rsicarelli 2014-06-13 15:49:00

3

好的親愛的在這裏,我把我的作物圖像的整個代碼在Android中。 這是全局變量。

//This For Image Crop 
     /** 
     * Uri for set image crop option . 
     */ 
     private Uri mImageCaptureUri; 
     /** 
     * int for set key and get key from result activity . 
     */ 
     public final int CROP_FROM_CAMERA = 0; 

/** 
    * Bitmap for apply Crop Operation Result. 
    */ 
    private Bitmap _tempOpration; 

    //This is Crop Method. 

/** 
    * Method for apply Crop . 
    * @param filePath - String path of file . 
    */ 
    private void doCrop(String filePath){ 
     try{ 
      //New Flow 
      mImageCaptureUri = Uri.fromFile(new File(filePath)); 

      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", 300); 
       intent.putExtra("outputY", 300); 
       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() 
        { 
         public void onCancel(DialogInterface dialog) 
         { 
          if (mImageCaptureUri != null) 
          { 
           getContentResolver().delete(mImageCaptureUri, null, null); 
           mImageCaptureUri = null; 
          } 
         } 
        }); 
        AlertDialog alert = builder.create(); 
        alert.show(); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      genHelper.showErrorLog("Error in Crop Function-->"+ex.toString()); 
     } 
    } 

這是另一類女巫使用了在應用程序來查找作物活動的意圖。

CropOption類。

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

這是用於顯示列表。

CropOptionAdapter

public class CropOptionAdapter extends ArrayAdapter<CropOption> 
{ 
    private ArrayList<CropOption> mOptions; 
    private LayoutInflater mInflater; 

    public CropOptionAdapter(Context context, ArrayList<CropOption> options) 
    { 
     super(context, R.layout.crop_selector, options); 

     mOptions = options; 

     mInflater = LayoutInflater.from(context); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup group) 
    { 
     if (convertView == null) 
      convertView = mInflater.inflate(R.layout.crop_selector, null); 

     CropOption item = mOptions.get(position); 

     if (item != null) { 
      ((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon); 
      ((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title); 

      return convertView; 
     } 

     return null; 
    } 
} 

佈局文件CropOptionAdapter

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:gravity="center_vertical"> 

    <ImageView 
     android:id="@+id/iv_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/tv_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=""/> 
</LinearLayout> 

這是resultActivity.witch給裁剪圖像。

/** 
    * @see android.app.Activity#onActivityResult(int, int, android.content.Intent) 
    */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode != RESULT_OK) return; 
     switch (requestCode) 
     { 
     case CROP_FROM_CAMERA: 
      if (data == null) 
      { 
       genHelper.showToast("No Crop Activity in This"); 
       return; 
      } 
      final Bundle extras = data.getExtras(); 
      if (extras != null) 
      { 
       try 
       { 

        _tempOpration=extras.getParcelable("data"); 
        imageLayout.setImageBitmap(_tempOpration); 
        _tempOpration=null; 

       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
      break; 
     } 

    } 

//這種類型它是在我的實時應用程序上工作。

genHelper.showToast(「No Crop Activity in This」);

是我的通用類,可幫助顯示吐司消息和錯誤日誌。

祝你好運。

+0

你開始加載作物活動,並加載失敗:\ – Datenshi 2013-03-08 06:52:50

+1

@Dennshi親愛的只是使用我的代碼它在我的實時應用程序中工作沒有任何問題。親愛的我給你我的完整code.just執行後say.and另一個這個我管理這件事情,如果沒有任何作物發現顯示toast.just檢查然後執行,然後審查。 – 2013-03-08 07:03:50

+0

我知道這可能是我的糟糕用法。我正在研究它。一個問題,你通過什麼樣的URI來做功能?目前我嘗試通過網址,或圖像應緩存到手機 – Datenshi 2013-03-08 07:08:42

1

正如在類似的線程中提到的,Android沒有官方的作物意圖 (https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html),所以我會遠離使用「com.android.camera.action.CROP」。

然而,自從這個問題最初發布以來,Android已經在Kitkat中添加了一個新的API(級別19),允許用戶召喚crop-this-image-and-set-as-wallpaper Activity。請參閱WallpaperManager.getCropAndSetWallpaperIntent(),這可能會解決您的原始問題。