将不同的两个图像添加到Firebase实时数据库

时间:2018-07-21 16:04:15

标签: java android firebase firebase-realtime-database

我想通过Android应用程序将两张不同的照片添加到数据库中。但是,当我为第一张照片选择照片时,它将为两张照片放置相同的照片并以相同的方式保存。我的代码在那儿。我为两张不同的照片定义了两个单独的imageview,但是它不起作用。我该怎么办?

 mDenemeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 1);
            }
        });


        mDenemeImage2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK);
                i.setType("image/*");
                startActivityForResult(i, 1);
            }
        });
     private void getUserInfo(){
        mDriverDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists() && dataSnapshot.getChildrenCount()>0){
                    Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();

                    if(map.get("profileDenemeUrl")!=null){
                        mprofileDenemeUrl = map.get("profileDenemeUrl").toString();
                        Glide.with(getApplication()).load(mprofileDenemeUrl).into(mDenemeImage);
                    }
                     if(map.get("profileDenemeUrl2")!=null){
                        mprofileDenemeUrl2 = map.get("profileDenemeUrl2").toString();
                        Glide.with(getApplication()).load(mprofileDenemeUrl2).into(mDenemeImage2);
                    }


                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }



    private void saveUserInformation() {



        Map userInfo = new HashMap();

        mDriverDatabase.updateChildren(userInfo);

        if(resultUri != null) {

            final StorageReference filePath = FirebaseStorage.getInstance().getReference().child("profile_arac").child(userID);
            final StorageReference filePath2 = FirebaseStorage.getInstance().getReference().child("profile_arac2").child(userID);

            Bitmap bitmap = null;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getApplication().getContentResolver(), resultUri);
            } catch (IOException e) {
                e.printStackTrace();
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
            byte[] data = baos.toByteArray();
            UploadTask uploadTask = filePath.putBytes(data);
            UploadTask uploadTask2 = filePath2.putBytes(data);
            uploadTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    finish();
                    return;
                }
            });
            uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            Map newImage = new HashMap();


                            newImage.put("profileDenemeUrl", uri.toString());
                            mDriverDatabase.updateChildren(newImage);

                            finish();
                            return;
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            finish();
                            return;
                        }
                    });

                }
            }); uploadTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    finish();
                    return;
                }
            });
            uploadTask2.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    filePath2.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            Map newImage2 = new HashMap();


                            newImage2.put("profileDenemeUrl2", uri.toString());
                            mDriverDatabase.updateChildren(newImage2);

                            finish();
                            return;
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            finish();
                            return;
                        }
                    });

                }
            }); uploadTask2.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    finish();
                    return;
                }
            });
        }
      else{
            finish();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == Activity.RESULT_OK){
            final Uri imageUri = data.getData();
            resultUri = imageUri;

            mDenemeImage.setImageURI(resultUri);

            mDenemeImage2.setImageURI(resultUri);
        }
    }

1 个答案:

答案 0 :(得分:0)

mDenemeImage.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_PICK); 
        intent.setType("image/*"); 
        startActivityForResult(intent, 1); 
    }
}); 

mDenemeImage2.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_PICK); 
        intent.setType("image/*"); startActivityForResult(i, 2); 
    }
}); 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 1 && resultCode == RESULT_OK){
        Uri imageUri1 = data.getData();
        resultUri1 = imageUri1;

        mDenemeImage.setImageURI(resultUri1);
    }

    if (requestCode == 2 && resultCode == RESULT_OK)
    {
        Uri imageUri2 = data.getData();
        resultUri2 = imageUri2;

        mDenemeImage2.setImageURI(resultUri2);
    }
}