简体   繁体   中英

How can I get the URI to the file with 3 different file choosers?

I have 3 different buttons: Upload Profile Picture, Upload Photo ID and Upload Criminal Record Photo, but after I call the openFileChooser() method which I created, I don't know how exactly I have to handle that to get 3 different URls.

I mean I did this

public class SignupCarrier extends AppCompatActivity {
    EditText editfullname, editemail, editpassword, editconfirmpassword, editaddress, editcity, editstate, editzipcode, editcountry, editphone, editcardnumber, editexpiredate, editcvc;
Button upProfile, upIDPhoto, upCriminalRecord;
private Uri mProfilePic, mIdPhoto,mCriminalRecord;
FirebaseAuth mFirebaseAuth;
private StorageReference mStorageRef;
private StorageTask mUploadTask;
    private static final int PICK_IMAGE_REQUEST = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup_carrier);


        mFirebaseAuth = FirebaseAuth.getInstance();
        upProfile = (Button) findViewById(R.id.profilePic);
        upIDPhoto = (Button) findViewById(R.id.idphotoPic);
        upCriminalRecord = (Button) findViewById(R.id.criminalRecord);

        mStorageRef = FirebaseStorage.getInstance().getReference("carriersPictures");

        upProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openFileChooser();
            }
        });
        upIDPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openFileChooser();
            }
        });
        upCriminalRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openFileChooser();
            }
        });

    }

    private void openFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }

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

        if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null)
        {
            mProfilePic = data.getData();


        }
    }
}


But this would probably work only for upProfile Button, how can I modify the onActivityResult to get for each buttons the URLs?

Thank you. I'm new to Android.

Add Uri as parameter to your openFileChooser() method.

private void openFileChooser(Uri uri)

and call it like this:

upProfile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openFileChooser(mProfilePic);
        }
    });

Update your openFileChooser() method to this:

private void openFileChooser(Uri uri) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    if (uri == mProfilePic) {
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    } else if (uri == mIdPhoto) {
        startActivityForResult(intent, PICK_IMAGE_REQUEST_ID);
    } else if (uri == mCriminalRecord) {
        startActivityForResult(intent, PICK_IMAGE_REQUEST_CR);
    }
}

You need to declare these constants: PICK_IMAGE_REQUEST_ID , PICK_IMAGE_REQUEST_CR

And in your onActivityResult() method check the requestCode to handle each case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM