当用户单击“上传”按钮时,我尝试上传多张图像。这是我的问题,当我单击“上传”时,将仅上传一个图像,并且我想一次上传两个图像。我不知道我在哪里做错。请解决
下面是我的代码
public class UserDocument extends AppCompatActivity {
Integer REQUEST_CAMERA = 1, SELECT_FILE = 0;
CardView dl_front, dl_back;
private static final int MY_CAMERA_REQUEST_CODE = 100;
ImageView dlFront, dlBack;
private boolean isDlBack = false;
private boolean isDlFront = false;
CustomerDocument currDocument;
Button btnUpload;
FirebaseStorage storage;
StorageReference storageReference;
Uri saveUri;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_document);
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();
dl_front = (CardView) findViewById(R.id.frameFrontSide_holder); // creating a CardView and assigning a value.
dl_back = (CardView) findViewById(R.id.frameBackSide_holder); // creating a CardView and assigning a value.
btnUpload = (Button) findViewById(R.id.btnUpload);
dlFront = (ImageView) findViewById(R.id.frameFrontSide);
dlBack = (ImageView) findViewById(R.id.frameBackSide);
dl_front.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isDlBack = false;
isDlFront = true;
// Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// startActivityForResult(intent,0);
SelectImage();
}
});
dl_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isDlFront = false;
isDlBack = true;
// Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// startActivityForResult(intent,0);
SelectImage();
}
});
if (checkSelfPermission(Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {
Manifest.permission.CAMERA
},
MY_CAMERA_REQUEST_CODE);
}
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
}
});
}
private void uploadImage() {
if (saveUri != null) {
final ProgressDialog mDialog = new ProgressDialog(this);
mDialog.setMessage("Uploading...");
mDialog.show();
String imageName = UUID.randomUUID().toString();
final StorageReference imageFolder = storageReference.child("images/" + imageName);
imageFolder.putFile(saveUri)
.addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mDialog.dismiss();
Toast.makeText(UserDocument.this, "Uploaded !!!", Toast.LENGTH_SHORT).show();
imageFolder.getDownloadUrl().addOnSuccessListener(new OnSuccessListener < Uri > () {
@Override
public void onSuccess(Uri uri) {
// currDocument = new CustomerDocument(Common.currentUser,uri.toString(),uri.toString());
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
mDialog.dismiss();
Toast.makeText(UserDocument.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}).addOnProgressListener(new OnProgressListener < UploadTask.TaskSnapshot > () {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
mDialog.setMessage("Uploaded " + progress + "%");
}
});
}
}
private void SelectImage() {
final CharSequence[] items = {
"Camera",
"Gallery",
"Cancel"
};
AlertDialog.Builder builder = new AlertDialog.Builder(UserDocument.this);
builder.setTitle("Add Image");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (items[i].equals("Camera")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[i].equals("Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
//startActivityForResult(intent.createChooser(intent, "Select File"), SELECT_FILE);
startActivityForResult(intent, SELECT_FILE);
} else if (items[i].equals("Cancel")) {
dialogInterface.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bundle bundle = data.getExtras();
final Bitmap bmp = (Bitmap) bundle.get("data");
if (isDlFront == true)
dlFront.setImageBitmap(bmp);
else if (isDlBack == true)
dlBack.setImageBitmap(bmp);
} else if (requestCode == SELECT_FILE) {
saveUri = data.getData();
if (isDlFront == true)
dlFront.setImageURI(saveUri);
else if (isDlBack == true)
dlBack.setImageURI(saveUri);
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
}
答案 0 :(得分:1)
这部分将覆盖saveUri
变量。
saveUri = data.getData();
if (isDlFront == true)
dlFront.setImageURI(saveUri);
else if (isDlBack == true)
dlBack.setImageURI(saveUri);
您可以声明另一个变量saveUrifront
和saveUriback
并将if语句更改为:
if (isDlFront == true)
saveUrifront = data.getData();
dlFront.setImageURI(saveUri);
else if (isDlBack == true)
saveUriback = data.getData();
dlBack.setImageURI(saveUri);
现在可以同时使用uploadImage()
和imageFolder.putFile()
的方法saveUrifront
调用saveUriback
并使用for循环或仅两次调用方法来上传图像。 / p>
答案 1 :(得分:0)
您的解决方案正在发挥作用。感谢您的答复@animeshk