图像裁剪活动不会将Image-Uri发送到接收活动(Android-Image-Cropper)

时间:2016-11-15 23:25:49

标签: android image android-imageview onactivityresult firebase-storage

我正在尝试裁剪image(我已从图库中选择或从相机中删除)并将其传递给另一个activity,该image应上传image到我的firebase存储。

首先(没有图像裁剪)一切都很顺利。我能够传递并上传onActivityResult ...但是在将图片裁剪代码添加到我的 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); mImageUri_gallery = data.getData(); mImageUri_camera = data.getData(); if (requestCode == 100 && resultCode == RESULT_OK) { CropImage.activity(mImageUri_gallery) .setGuidelines(CropImageView.Guidelines.ON) .start(this); if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { CropImage.ActivityResult result = CropImage.getActivityResult(data); if (resultCode == RESULT_OK) { Uri resultUriGallery = result.getUri(); Intent intentGallery = new Intent(MainActivity.this, ImagePostActivity.class); intentGallery.putExtra("image-uri", resultUriGallery.toString()); startActivity(intentGallery); } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) { Exception error = result.getError(); } } } else if (resultCode != RESULT_CANCELED && data != null) { if (requestCode == 200 && resultCode == RESULT_OK) { CropImage.activity(mImageUri_camera) .setGuidelines(CropImageView.Guidelines.ON) .start(this); if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { CropImage.ActivityResult result = CropImage.getActivityResult(data); if (resultCode == RESULT_OK) { Uri resultUriCamera = result.getUri(); Intent intentCamera = new Intent(MainActivity.this, ImagePostActivity.class); intentCamera.putExtra("image-uri", resultUriCamera.toString()); startActivity(intentCamera); } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) { Exception error = result.getError(); } } } } } 后,只需按下' crop'后关闭imageCropActivity即可。并跳回我的MainActivity。

我已经尝试但我真的无法弄清楚它为什么这样做。也许我错过了一些东西。有任何想法吗?

对于图像裁剪我使用此库:https://github.com/ArthurHub/Android-Image-Cropper

主要活动

public class ImagePostActivity extends AppCompatActivity {

    public static TextView txt_btn_post;
    public static EditText mPostTitle;
    public static ImageView imagePostPreview;
    private StorageReference mStorage;
    private DatabaseReference mDatabase;
    private ProgressDialog mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_post);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mStorage = FirebaseStorage.getInstance().getReference();
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Test_Stream_Uploads");
        mProgress = new ProgressDialog(this);
//        mPostTitle = (EditText) findViewById(R.id.mPostTitle);
        imagePostPreview = (ImageView) findViewById(R.id.imagePostPreview);
        Intent intent = getIntent();

        mImageUri_gallery = Uri.parse(intent.getStringExtra("image-uri"));
        imagePostPreview.setImageURI(mImageUri_gallery);
        mImageUri_camera = Uri.parse(intent.getStringExtra("image-uri"));
        imagePostPreview.setImageURI(mImageUri_camera);

        txt_btn_post = (TextView) findViewById(R.id.txt_btn_post);
        txt_btn_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startPosting();
            }
        });


    }

    private void startPosting() {

        mProgress.setMessage("Bild wird hochgeladen...");
        mProgress.show();
        if (mImageUri_gallery != null) {
            StorageReference filepath = mStorage.child("Stream_Image").child(mImageUri_gallery.getLastPathSegment());
            filepath.putFile(mImageUri_gallery).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Uri downloadUrl = taskSnapshot.getDownloadUrl();
                    Intent intent = new Intent(getBaseContext(), MainActivity.class);
                    DatabaseReference newPost = mDatabase.push();
                    newPost.child("image").setValue(downloadUrl.toString());
                    mProgress.dismiss();
                    startActivity(intent);
                }
            });
        }


    }
}

ImagePostActivity

dict

1 个答案:

答案 0 :(得分:0)

我也积极使用这个工具而且我实际上并没有真正进入你的代码,但我可以为你提供一个如何为我工作的例子。如果你研究它,你可以轻松地跟随并得到这个想法。它适用于您从图库中拍摄的图像。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
        Uri imageUri = data.getData();
        CropImage.activity(imageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(144,81)
                .start(this);
    }
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            ib_image.setImageURI(resultUri);
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
        }
    }
}

如果你想将它应用到你刚刚从相机拍摄的图像上,你需要创建自己的uri,这有点长,但是在Android文档中对它进行了解释:(https://developer.android.com/training/camera/photobasics.html#TaskPhotoView),我在这里的讨论中解释过:Image capture with camera & upload to Firebase (Uri in onActivityResult() is null)