我正在尝试裁剪一个image
(我从图库中选择或从相机中拍摄(并将其传递给另一个activity
,该应该将image
上传到我的Firebase存储中。
起初(没有图像裁剪(,一切都很顺利。我能够通过并上传image
...但是在将图像裁剪代码添加到我的onActivityResult
后,它只是在按"裁剪"后关闭图像裁剪活动并跳回我的主活动。
我已经尝试过,但我真的不知道为什么它会这样做。也许我错过了一些东西。有什么想法吗?
对于图像裁剪,我正在使用这个库:https://github.com/ArthurHub/Android-Image-Cropper
主要活动
@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();
}
}
}
}
}
图像后活动
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);
}
});
}
}
}
我也在积极使用这个工具,实际上我并没有真正进入你的代码,但我可以给你一个例子,说明它是如何为我工作的。如果你研究它,你可以很容易地遵循并得到这个想法。它适用于您从图库中拍摄的图像。
@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(,我已经在这里的讨论中解释过:使用相机捕获图像并上传到 Firebase(onActivityResult(( 中的 Uri 为空(