2017-08-07 12 views
2

私はこのCropライブラリをSoundCloudで使用しています。 1つの画像ビューを選択し、画像を選択し、トリミングして画像ビューに結果を表示すると問題はありません。しかし今、私は異なる仕様の2つの異なる画像ビューでそれをしようとしています。私はどんなエラーも出ていないし、何か結果を見ている。ここで私が試したことは次のとおりです。Androidで異なるパラメータを使用して同じアクティビティで2つの異なる画像を切り抜くにはどうすればよいですか?

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

私はそれを考えましたが、トリミングライブラリのクラス "Crop"は、返されたクロップされたイメージに対してデフォルトでREQUEST_CROP requestCodeを呼び出します。私が知らないのは、彼らが異なっている必要がある場合、二つを分ける方法です。 –

+0

ライブラリでは、最初の "select image"リクエストに対してカスタムrequestCodeを渡すことができますが、同じアクティビティ内から2つの異なる作物の返されたイメージを取得することについてはどうでしょうか? 2つの異なるクロップリクエストコードを呼び出すようにクロップクラスを変更できると思いますか? –

+0

Cropには#start()メソッドのバリエーションが多数あり、Context、Fragment、カスタムリクエストコードを取り入れています。私はそれがあなたが必要とするものだと思います。 – Vibin

1
  1. リスト項目

は、いくつかの変数が私のためとして余分に使用されている。ここ改造ライブラリで、残りのクライアントを使用してサーバーにトリミングし、アップロードする画像の選択の完全なコードで参照してください。また、私はgradel

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の内側に、私は<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()としてこれをspecifedています。 //文字列hhhh = helloWorld.substring(helloWorld.indexOf( ":")+ 1、helloWorld.indexOf( "/")); //文字列名= "snehal_go"; savedFileDestination =新しいファイル(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); 
} 
関連する問題