2017-12-13 17 views
0

を取得し、私は以下のようなアンドロイドの意図を使用して私のアプリでトリミング画像を実装している:出力トリミングした画像URI

CropIntent = new Intent("com.android.camera.action.CROP"); 

私はトリミングされた画像を受信し、私のUIに表示されますが、私は、アップロードのような更なるユースケースを実行したいしています切り取った画像URI。問題は、活動にuriが返されないことです。

は、ここに私のクロップ画像意図抜粋です:

galleryFAB.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Log.d(TAG, "Gallery Btn Clicked"); 
      selectFromGallery(); 
     } 
    }); 

private void selectFromGallery() { 

    galleryIntent = new Intent(Intent.ACTION_GET_CONTENT); 
    galleryIntent.setType("image/*"); 
    startActivityForResult(Intent.createChooser(galleryIntent, "Choose From"), 2); 
} 

活動結果の方法で私のコード:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    InputStream inputStream; 

    if (requestCode == 2 && resultCode == RESULT_OK){ 
     if (data != null){ 
      cropImageUri = data.getData(); 
      userImage.setImageURI(cropImageUri); 
      try { 
       originalBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), cropImageUri); 

       inputStream = getContentResolver().openInputStream(data.getData()); 
       originalBitmap = BitmapFactory.decodeStream(inputStream); 

       Log.d(TAG, "Original bitmap byte count is:\t" + originalBitmap.getByteCount()); 

       resizedBitmap = getResizedBitmap(originalBitmap, 200); 

       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 

       Log.d(TAG, "Resized bitmap byte count is:\t" + resizedBitmap.getByteCount()); 

       try { 
        File file = saveBitmap("avatar_image"); 
        upLoadFile(file); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 


       byte[] bytes = stream.toByteArray(); 

       /** 
       * For Shared Preferences 
       * **/ 

       encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT); 

       imagePrefs = getSharedPreferences("ImagePrefs", Context.MODE_PRIVATE); 
       SharedPreferences.Editor editor = imagePrefs.edit(); 
       editor.putString("image_user", encodedImage).commit(); 

       upLoadBytes(bytes); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      cropImage(); 
     } 

    } else if (requestCode == PERM_CODE){ 
     if (data != null){ 

      //I want to get the cropped image uri here but unable to. Have tried converting the bitmap rcvd here to uri but in activity, ntn is returned. kindly guide me 

      Bundle bundle = data.getExtras(); 
      originalBitmap = bundle.getParcelable("data"); 
      userImage.setImageBitmap(resizedBitmap); 
     } 

    } 

} 

私のクロップ画像機能:

private void cropImage() { 
    try { 
     CropIntent = new Intent("com.android.camera.action.CROP"); 

     CropIntent.setDataAndType(cropImageUri, "image/*"); 
     CropIntent.putExtra("crop", true); 
     CropIntent.putExtra("outputX", 200); 
     CropIntent.putExtra("outputY", 200); 
     CropIntent.putExtra("aspectX", 1); 
     CropIntent.putExtra("aspectY", 1); 
     CropIntent.putExtra("scale", true); 
     CropIntent.putExtra("return-data", true); 

     startActivityForResult(CropIntent, PERM_CODE); 


    } catch (ActivityNotFoundException ex){ 

    } 
} 

は誰でも取得する方法を言うことができます出力uri?ありがとう。

+0

Androidには 'CROP'' Intent'がありません。多くの[Android用の画像トリミングライブラリ](https://android-arsenal.com/tag/45)があります。 1つを使用してください。 – CommonsWare

答えて

0

クロップされたイメージがImageViewに表示されている場合は、this linkに従ってデバイスストレージに保存してからファイルをアップロードします。

関連する問題