im使用Android将3张图像上传到Firebase存储,但是当我选择单击第一个图像选择时,它将在其他两个图像上显示相同的图像,但是我需要上传不同的图像。请帮助
private Toolbar newPostToolbar;
private ImageView newPostImage;
private ImageView newPostImage2;
private ImageView newPostImage3;
private EditText newPostDesc;
private Button newPostBtn;
private Uri postImageUri = null;
private Uri postImageUri2 = null;
private Uri postImageUri3 = null;
private ProgressBar newPostProgress;
private StorageReference storageReference;
private FirebaseFirestore firebaseFirestore;
private FirebaseAuth firebaseAuth;
private String current_user_id;
private Bitmap compressedImageFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_post);
storageReference = FirebaseStorage.getInstance().getReference();
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseAuth = FirebaseAuth.getInstance();
current_user_id = firebaseAuth.getCurrentUser().getUid();
newPostToolbar = findViewById(R.id.new_post_toolbar);
setSupportActionBar(newPostToolbar);
getSupportActionBar().setTitle("Images");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
newPostImage = findViewById(R.id.new_post_image);
newPostImage2 = findViewById(R.id.new_post_image2);
newPostImage3 = findViewById(R.id.new_post_image3);
newPostDesc = findViewById(R.id.new_post_desc);
newPostBtn = findViewById(R.id.post_btn);
newPostProgress = findViewById(R.id.new_post_progress);
newPostImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setMinCropResultSize(512, 512)
.setAspectRatio(1, 1)
.start(NewPostActivity.this);
}
});
newPostImage2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setMinCropResultSize(512, 512)
.setAspectRatio(1, 1)
.start(NewPostActivity.this);
}
});
newPostImage3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setMinCropResultSize(512, 512)
.setAspectRatio(1, 1)
.start(NewPostActivity.this);
}
});
newPostBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String desc = newPostDesc.getText().toString();
if(!TextUtils.isEmpty(desc) && postImageUri != null){
newPostProgress.setVisibility(View.VISIBLE);
final String randomName = UUID.randomUUID().toString();
// PHOTO UPLOAD
File newImageFile = new File(postImageUri.getPath());
try {
compressedImageFile = new Compressor(NewPostActivity.this)
.setMaxHeight(720)
.setMaxWidth(720)
.setQuality(50)
.compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageData = baos.toByteArray();
// PHOTO UPLOAD
UploadTask filePath = storageReference.child("post_images").child(randomName + ".jpg").putBytes(imageData);
filePath.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task) {
final String downloadUri = task.getResult().getDownloadUrl().toString();
if(task.isSuccessful()){
File newThumbFile = new File(postImageUri.getPath());
try {
compressedImageFile = new Compressor(NewPostActivity.this)
.setMaxHeight(100)
.setMaxWidth(100)
.setQuality(1)
.compressToBitmap(newThumbFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] thumbData = baos.toByteArray();
UploadTask uploadTask = storageReference.child("post_images/thumbs")
.child(randomName + ".jpg").putBytes(thumbData);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String downloadthumbUri = taskSnapshot.getDownloadUrl().toString();
Map<String, Object> postMap = new HashMap<>();
postMap.put("image_url", downloadUri);
postMap.put("image_thumb", downloadthumbUri);
postMap.put("desc", desc);
postMap.put("user_id", current_user_id);
postMap.put("timestamp", FieldValue.serverTimestamp());
firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
if(task.isSuccessful()){
Toast.makeText(NewPostActivity.this, "Post was added", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(NewPostActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
}
newPostProgress.setVisibility(View.INVISIBLE);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//Error handling
}
});
} else {
newPostProgress.setVisibility(View.INVISIBLE);
}
}
});
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
postImageUri = result.getUri();
newPostImage.setImageURI(postImageUri);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
postImageUri2 = result.getUri();
newPostImage2.setImageURI(postImageUri2);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
postImageUri3 = result.getUri();
newPostImage3.setImageURI(postImageUri3);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}
以下是我的XML代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".NewPostActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/new_post_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.Toolbar>
<ProgressBar
android:id="@+id/new_post_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/new_post_image" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:text="Attach Photo of the conman or the company logo"
android:textColor="@color/colorPrimary" />
<ImageView
android:id="@+id/new_post_image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/new_post_toolbar"
app:srcCompat="@drawable/post_placeholder" />
<EditText
android:id="@+id/new_post_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:ems="10"
android:background="@drawable/input_outline"
android:hint="Description (What happened)"
android:inputType="textMultiLine"
android:padding="14dp"
android:textSize="14sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:text="Attach any other Supporting Photo"
android:textColor="@color/colorPrimary" />
<ImageView
android:id="@+id/new_post_image2"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/new_post_toolbar"
app:srcCompat="@drawable/post_placeholder" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Attach below a photo"
android:textColor="@color/colorPrimary" />
<ImageView
android:id="@+id/new_post_image3"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/new_post_toolbar"
app:srcCompat="@drawable/post_placeholder" />
<Button
android:id="@+id/post_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:backgroundTint="@color/colorAccent"
android:padding="16dp"
android:text="@string/new_post_btn_hint"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
请帮助,我被这个项目困扰了几个月。我认为我的问题是如何获取图像uri并将其显示在相对的imageview上。