我想用裁剪库(https://github.com/ArthurHub/Android-Image-Cropper)选择一张照片,然后我希望它存储在Firebase中。
问题是,在我选择默认图库上的图片后,裁剪未打开且图片未保存在Firebase存储上。 我犯了什么错? (我在代码中删除了不重要的东西)
package com.cryneo.lobby;
import android.content.Intent;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import de.hdodenhof.circleimageview.CircleImageView;
public class SettingsActivity extends AppCompatActivity {
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
private CircleImageView mImage;
private Button mImageBtn;
private static final int GALLERY_PICK = 1;
private StorageReference mImageStorage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
mImage = (CircleImageView) findViewById(R.id.settings_image);
mImageBtn = (Button) findViewById(R.id.settings_image_btn);
mImageStorage = FirebaseStorage.getInstance().getReference();
String current_uid = mCurrentUser.getUid();
mImageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryintent = new Intent();
galleryintent.setType("image/*");
galleryintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryintent,"Bild auswählen"), GALLERY_PICK);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_PICK && requestCode == RESULT_OK) {
Uri imageUri = data.getData();
CropImage.activity(imageUri)
.setAspectRatio(1,1)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
StorageReference filepath = mImageStorage.child("profile_images").child("profile_image.jpg");
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()){
Toast.makeText(SettingsActivity.this,"Perfekt", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(SettingsActivity.this,"Fehler", Toast.LENGTH_LONG).show();
}
}
});
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}
答案 0 :(得分:0)
好的,现在有效。我刚做了一个错字。而不是requestCode,它必须是resultCode
if (requestCode == GALLERY_PICK && resultCode== RESULT_OK) {