2017-08-07 73 views
2

我正在使用SoundCloud的Crop庫。選擇一個圖像視圖,選擇圖像,裁剪並在圖像顯示結果工作正常。但現在我試圖用兩種不同規格的不同圖像瀏覽。我沒有收到任何錯誤,也沒有看到任何結果。以下是我所嘗試的:如何在Android中使用不同參數在同一活動中裁剪兩張不同的圖像?

//My click listeners 
regCoverPhoto.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Crop.pickImage(getActivity(), EditProfileDialog.this, REQUEST_CODE_COVER); 
    } 
}); 
regUserProfile.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Crop.pickImage(getActivity(), EditProfileDialog.this, REQUEST_CODE_PROFILE); 
    } 
}); 
//Handling the result 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_CODE_PROFILE && resultCode == Activity.RESULT_OK) { 
     beginCropProfile(data.getData()); 
    }else if(requestCode == REQUEST_CODE_COVER && resultCode == Activity.RESULT_OK){ 
     beginCropCover(data.getData()); 
    } else if (requestCode == Crop.REQUEST_CROP) { 
     handleCrop(requestCode, resultCode, data); 
    } 
} 

private void beginCropProfile(Uri source) { 
    Uri destination = Uri.fromFile(new File(getActivity().getCacheDir(), "cropped")); 
    Crop.of(source, destination).withAspect(ASPECT_X, ASPECT_Y).start(getActivity(), EditProfileDialog.this, REQUEST_CODE_COVER); 
} 

private void beginCropCover(Uri source) { 
    Uri destination = Uri.fromFile(new File(getActivity().getCacheDir(), "cropped")); 
    Crop.of(source, destination).asSquare().start(getActivity(), EditProfileDialog.this, REQUEST_CODE_PROFILE); 
} 

private void handleCrop(int requestCode, int resultCode, Intent result) { 
    if (requestCode == REQUEST_CODE_COVER && resultCode == Activity.RESULT_OK) { 
     regCoverPhoto.setImageURI(Crop.getOutput(result)); 

     mCoverPhotoUri = Crop.getOutput(result); 
     uploadCoverToStorage(); 

     Log.d(TAG,"ResultCover: " + Crop.getOutput(result).toString()); 
    }else if(requestCode == REQUEST_CODE_PROFILE && resultCode == Activity.RESULT_OK){ 
     regUserProfile.setImageURI(Crop.getOutput(result)); 
     mProfilePhotoUri = Crop.getOutput(result); 
     uploadProfileToStorage(); 
     Log.d(TAG,"ResultProfile: " + Crop.getOutput(result).toString()); 
    } else if (resultCode == Crop.RESULT_ERROR) { 
     Snackbar.make(getView(), Crop.getError(result).getMessage(), Snackbar.LENGTH_LONG).show(); 
    } 
} 
+0

我只是想知道,如果你想一次或一個接一個地裁剪兩個圖像????用不同的onclick聽衆? @Steve C –

+0

@SnehalGongle一個接一個 –

回答

1

我沒有特別使用這個庫,但自定義請求代碼似乎是問題所在。

使用不同的請求代碼進行揀選和裁剪(總共4個請求代碼),因爲您需要以不同方式處理它們,並更新onActivityResult()handleCrop()以反映這一點。

https://gist.github.com/vibinr/fcf54c5e7ab63b9184432cc44c9a1494

+0

我認爲,但裁剪庫中的類「裁切」會爲返回的裁剪圖像默認調用REQUEST_CROP requestCode。我不知道如何區分兩者,如果他們需要不同。 –

+0

該庫指出您可以爲初始「選擇圖像」請求傳遞一個自定義requestCode,但是如何從同一活動中獲取兩個不同作物的返回圖像?你認爲我可以修改Crop類來調用兩個不同的作物請求代碼嗎? –

+0

我可以看到Crop有許多其#start()方法的變體,它需要上下文,片段和自定義請求代碼。我認爲這就是你需要的。 – Vibin

1
  1. 列表項

下面是使用REST客戶端與改造庫的圖像選擇的完整代碼裁剪和上傳到服務器下面幾個變量使用額外的爲我的使用請忽略他們

而且我使用內部格拉德爾這些庫

compile 'com.squareup.retrofit2:retrofit:2.3.0' 
compile 'com.theartofdev.edmodo:android-image-cropper:2.4.+' 
compile 'com.squareup.retrofit:retrofit:1.6.1' 

而且內部Mainfrest我已經specifed此作爲<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity" android:screenOrientation="portrait" />

side2Image.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
    image_name = "image_side_2"; 
    image_number_5 = "image_side_2"; 
    imagePath = Environment.getExternalStorageDirectory().toString(); 
    new File(imagePath).mkdir(); 

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss"); 
    String name = dateFormat.format(new Date()); 
    savedFileDestination = new File(imagePath, name + ".jpg"); 

    CropImage.activity(Uri.fromFile(savedFileDestination)); 
    CropImage.startPickImageActivity(TradeAddProduct.this); 
} 

backImage.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
    image_name = "image_back"; 
    image_number_6 = "image_back"; 
    imagePath = Environment.getExternalStorageDirectory().toString(); 
    new File(imagePath).mkdir(); 

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss"); 
    String name = dateFormat.format(new Date()); 
    savedFileDestination = new File(imagePath, name + ".jpg"); 

    CropImage.activity(Uri.fromFile(savedFileDestination)); 
    CropImage.startPickImageActivity(TradeAddProduct.this); 
} 

@Override 
@SuppressLint("NewApi") 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) { 
     imageUri = CropImage.getPickImageResultUri(this, data); 

     // For API >= 23 we need to check specifically that we have permissions to read external storage. 
     if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) { 
      // request permissions and handle the result in onRequestPermissionsResult() 
      mCropImageUri = imageUri; 
      requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 0); 
     } else { 
      // no permissions required or already grunted, can start crop image activity 
      startCropImageActivity(imageUri); 
     } 
    } 

    // handle result of CropImageActivity 
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { 
     CropImage.ActivityResult result = CropImage.getActivityResult(data); 
     if (resultCode == RESULT_OK) { 

      if (image_name.equals("image_front")) { 
       image.setImageURI(result.getUri()); 
      } else if (image_name.equals("image_top")) { 
       topImage.setImageURI(result.getUri()); 
      } else if (image_name.equals("image_bottom")) { 
       bottomImage.setImageURI(result.getUri()); 
      } else if (image_name.equals("image_side_1")) { 
       side1Image.setImageURI(result.getUri()); 
      } else if (image_name.equals("image_side_2")) { 
       side2Image.setImageURI(result.getUri()); 
      } else if (image_name.equals("image_back")) { 
       backImage.setImageURI(result.getUri()); 
      } 

      cropped = result.getUri(); 
      File path = getExternalCacheDir(); 
      new File(String.valueOf(path)).mkdir(); 

      imagePath = Environment.getExternalStorageDirectory().toString(); 
      new File(imagePath).mkdir(); 

      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss"); 
      String name22 = dateFormat.format(new Date()); 

//字符串的helloWorld = cropped.toString(); // String hhhh = helloWorld.substring(helloWorld.indexOf(「:」)+ 1,helloWorld.indexOf(「/」)); // String name =「snehal_go」; savedFileDestination = new File(imagePath,name22 +「.jpg」);

  ContextWrapper cw = new ContextWrapper(getApplicationContext()); 
      // path to /data/data/yourapp/app_data/imageDir 
      File directory = cw.getDir("Webmirchi..", Context.MODE_PRIVATE); 

      String ALLOWED_CHARACTERS = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"; 
      Random generator = new Random(); 
      randomStringBuilder = new StringBuilder(); 
      int randomLength = 10; 
      for (int i = 0; i < randomLength; i++) { 
       randomStringBuilder.append(ALLOWED_CHARACTERS.charAt(generator.nextInt(ALLOWED_CHARACTERS.length()))); 
      } 


      // Create imageDir 
      mypath = new File(directory, sessionMail + "-" + ProductId + ".jpg"); 

      FileOutputStream fos = null; 
      try { 
       Bitmap bitmapImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), cropped); 

       fos = new FileOutputStream(mypath); 
       // Use the compress method on the BitMap object to write image to the OutputStream 
       bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); 
       Log.i("File", mypath.toString()); 


       if (image_name.equals("image_front")) { 
        uploadImage_front(); 
       } else if (image_name.equals("image_top")) { 
        uploadImage_top(); 
       } else if (image_name.equals("image_bottom")) { 
        uploadImage_bottom(); 
       } else if (image_name.equals("image_side_1")) { 
        uploadImage_side1(); 
       } else if (image_name.equals("image_side_2")) { 
        uploadImage_side2(); 
       } else if (image_name.equals("image_back")) { 
        uploadImage_back(); 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        fos.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 


     } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) { 
      // Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
@Override 
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
    if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
     // required permissions granted, start crop image activity 
     startCropImageActivity(mCropImageUri); 
    } else { 
     Toast.makeText(this, "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show(); 
    } 
} 
    */ 
private void startCropImageActivity(Uri imageUri) { 
    CropImage.activity(imageUri) 
      .setGuidelines(CropImageView.Guidelines.ON) 
      .setMultiTouchEnabled(false) 
      .setMinCropWindowSize(600, 600) 
      .setAspectRatio(2, 2) 
      .setRequestedSize(500, 500) 
      .start(this); 
} 
相關問題