2017-07-24 65 views
0

當我不使用作物活動時,我有一堆代碼可以正常工作 但我想將裁剪應用到所選圖像並使用Rest API將其發送到服務器使用rest api存儲數據庫中的Android作物圖像

TypedFile typedFile = new TypedFile("multipart/form-data",savedFileDestination); 
initiateProgressDialog(); 

如何設置剪裁的URI而不是savedFileDestination,以便它將裁剪後的圖像路徑作爲文件?

+0

請參閱此鏈接設置屬性來裁剪圖像工具@ Snehal Gongle: - http s://stackoverflow.com/a/41121904/4906130,你會在Util.REQUEST_CROP_IMAGE – Bhavnik

+0

中發現croppedImageUri我實際上使用了這個庫,它使我能夠裁剪圖像並設置圖像的高度和寬度compile'c​​om .theartofdev.edmodo:android-image-cropper:2.4。+'所以我不必爲GALLERY和Camera提供單獨的代碼,這個庫對我來說確實有效...... –

+0

所以我的大問題是我無法設置這個圖像的路徑是 file:/data/user/0/com.snake.againimage/cache/cropped560934775.jpg類似這樣的東西讓我錯誤爲E/Upload:file:/ data/user/0/com.snake.againimage/cache/cropped560934775.jpg:打開失敗:ENOENT(沒有這樣的文件或目錄) –

回答

0

使用下面的方法來裁剪:

public static Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) { 
    Bitmap bm = null; 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    bm = BitmapFactory.decodeFile(path, options); 

    return bm; 
} 



public static int calculateInSampleSize(

     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
      inSampleSize = Math.round((float) height/(float) reqHeight); 
     } else { 
      inSampleSize = Math.round((float) width/(float) reqWidth); 
     } 
    } 

    return inSampleSize; 
} 
0

感謝您的答案和意見,我發現我的解決方案是,我有我的croped圖像先存儲到文件diectory因爲croped圖像存儲高速緩存存儲器,我們需要的文件路徑和發送的文件路徑服務器來存儲它.....再次感謝

0

在相機

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       File outPutFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "Path of your custom directory); 
       if (!outPutFile.exists()) { 
        outPutFile.mkdirs(); 
       } 
       Uri capturedImageUri = Uri.fromFile(File.createTempFile("Your app directory name" + System.currentTimeMillis(), ".jpg", outPutFile)); 
       Logg.e(getClass().getSimpleName(), "Captured_Pic ===== " + Uri.fromFile(outPutFile)); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri); 
       startActivityForResult(intent, Util.REQUEST_CAMERA); 
的點擊

在畫廊的點擊

CropImage.startPickImageActivity(HomeActivity.this); 

OnActivityResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != RESULT_CANCELED) { 
     switch (requestCode) { 
      case Util.REQUEST_CAMERA: // Camera request 
       startCropImageActivity(capturedImageUri); 
       break; 

      case CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE: // Crop 
       CropImage.ActivityResult result = CropImage.getActivityResult(data); 
       try { 
        if (resultCode == RESULT_OK) { 
         resultUri = result.getUri(); 
         mProfileView.setImageURI(Uri.parse(resultUri.toString())); // this is my imageview, where I'll set that cropped image Uri. 
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) { 
         Exception error = result.getError(); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       break; 

      case CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE: // Gallery request 
       try { 
        Uri selectedImageUri = CropImage.getPickImageResultUri(this, data); 
        startCropImageActivity(selectedImageUri); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       break; 
     } 
    } 
} 

此方法將根據您的要求

private void startCropImageActivity(Uri imageUri) { 
    CropImage.activity(imageUri) 
      .setGuidelines(CropImageView.Guidelines.ON) 
      .setCropShape(CropImageView.CropShape.RECTANGLE) 
      .setActivityMenuIconColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue)) 
      .setGuidelinesColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue)) 
      .setScaleType(CropImageView.ScaleType.FIT_CENTER) 
      .setFixAspectRatio(true) 
      .setBorderCornerColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue)) 
      .setBorderLineColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue)) 
      .start(this); 
}