简体   繁体   English

如何使用 3 个不同的文件选择器获取文件的 URI?

[英]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.我有 3 个不同的按钮:上传个人资料图片、上传照片 ID 和上传犯罪记录照片,但是在调用我创建的 openFileChooser() 方法后,我不知道我必须如何处理才能获得 3 个不同的 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?但这可能仅适用于 upProfile 按钮,如何修改 onActivityResult 以获取每个按钮的 URL?

Thank you.谢谢你。 I'm new to Android.我是安卓新手。

Add Uri as parameter to your openFileChooser() method.Uri作为参数添加到您的openFileChooser()方法。

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:将您的openFileChooser()方法更新为:

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您需要声明这些常量: PICK_IMAGE_REQUEST_IDPICK_IMAGE_REQUEST_CR

And in your onActivityResult() method check the requestCode to handle each case并在您的onActivityResult()方法中检查requestCode以处理每种情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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