如何在通过意图传递的 onActivityResult() 中获取 stringExtra?

how to get stringExtra in onActivityResult() that is passed through intent?

我正在尝试通过外部将照片上传到 firebase 存储 storage.There 有 2 种类型的照片,一种是考试,另一种是我的应用程序中的时间表。如何在 upload_timetable 或 upload_exam 时传递 stringExtra 以便 onActivityResult() 我可以弄清楚它来自 upload_timetable 或 upload_exam.

我是这样实现的。但是如果我点击 upload_timetable 或 upload_exam 我总是在 toast 中出错 message.Help 拜托我

我传递字符串的代码

public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            switch (id)
            {
                case android.R.id.home:
                    this.finish();
                    return true;
                case R.id.upload_timetable:
                    if(admin && isStoragePermissionGranted()) {
                        Intent intents = new Intent(Intent.ACTION_PICK);
                        intents.setType("image/*");
                        intents.putExtra(sExamType,"timetable");
                        setResult(RESULT_OK,intents);
                        startActivityForResult(intents, GALLERY_INTENT);
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(),"Action denied",Toast.LENGTH_SHORT).show();
                    }
                        return true;
                case R.id.view_photo:
                    mStorageRef.child(college).child(branch).child(year).child(section).child("timetable").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            // Got the download URL for 'users/me/profile.png'
                            showPhoto(uri);
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            // Handle any errors
                            Toast.makeText(getApplicationContext(),"Photo Doesnt Exists!",Toast.LENGTH_SHORT).show();
                        }
                    });
                    return true;

                case R.id.view_exam:
                    mStorageRef.child(college).child(branch).child(year).child(section).child("exam").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            // Got the download URL for 'users/me/profile.png'
                            showPhoto(uri);
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            // Handle any errors
                            Toast.makeText(getApplicationContext(),"Photo Doesnt Exists!",Toast.LENGTH_SHORT).show();
                        }
                    });
                    return true;

                case R.id.upload_exam:
                    if(admin && isStoragePermissionGranted()) {
                        Intent intentsd = new Intent(Intent.ACTION_PICK);
                        intentsd.putExtra(sExamType,"exam");
                        intentsd.setType("image/*");
                        setResult(RESULT_OK,intentsd);
                        startActivityForResult(intentsd, GALLERY_INTENT);
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(),"Action denied",Toast.LENGTH_SHORT).show();
                    }
                    return true;


            }
            return super.onOptionsItemSelected(item);
        }

我检索传递的字符串的代码

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


            if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK)
            {
                String sTypes = data.getStringExtra(sExamType);
                StorageReference reference;
                if(TextUtils.equals(sTypes,"timetable") && !TextUtils.isEmpty(sTypes) {
                    reference = mStorageRef.child(college).child(branch).child(year).child(section).child("timetable");
                }else if(TextUtils.equals(sTypes,"timetable") && !TextUtils.isEmpty(sTypes)
                {
                    reference = mStorageRef.child(college).child(branch).child(year).child(section).child("exam");
                }else
{
 Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_SHORT).show():
}
                progressDialog.setMessage("Image Uploading....");
                progressDialog.show();
                Uri uri = data.getData();
                reference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Toast.makeText(getApplicationContext(),"Photo Uploaded!!!",Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(getApplicationContext(),"Unable to Upload!!!",Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                    }
                });
            }
        } 

而不是只有一个请求代码 (GALLERY_INTENT) 两个 startActivityForResult,创建 2 个不同的代码,这样在 onActivityResult 中你会得到它并且你可以验证。

创建 2 个请求代码

private static final int GALLERY_INTENT_TIME_TABLE = 201;
private static final int GALLERY_INTENT_EXAM = 202;

征集时间table

Intent intents = new Intent(Intent.ACTION_PICK);
intents.setType("image/*");
intents.putExtra(sExamType,"timetable");
setResult(RESULT_OK,intents);
startActivityForResult(intents, GALLERY_INTENT_TIME_TABLE);

考试

    Intent intentsd = new Intent(Intent.ACTION_PICK);
    intentsd.putExtra(sExamType,"exam");
    intentsd.setType("image/*");
    setResult(RESULT_OK,intentsd);
    startActivityForResult(intentsd, GALLERY_INTENT_EXAM);

onActivityResult 代码

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


        if ((requestCode == GALLERY_INTENT_TIME_TABLE || requestCode == GALLERY_INTENT_EXAM) && resultCode == RESULT_OK) {

            if(requestCode == GALLERY_INTENT_TIME_TABLE) {
                //do timetable operation
            } else if(requestCode == GALLERY_INTENT_EXAM) {
                //do exam operation
            }

        }
    }

首先在您的代码中定义密钥的地方 "sExamType" 它应该类似于

String sExamType="SExamTypeKey";

然后在你的 onActivityResult 中写

String sTypes = data.getStringExtra(sExamType);

没关系,这是检索额外字符串的正确方法,但如果语句相同,则检查 "timetable" 而不是 "exam"。

StorageReference reference;
                if(TextUtils.equals(sTypes,"timetable") && !TextUtils.isEmpty(sTypes) {
                    reference = mStorageRef.child(college).child(branch).child(year).child(section).child("timetable");
                }else if(TextUtils.equals(sTypes,"timetable") && !TextUtils.isEmpty(sTypes)
                {
                    reference = mStorageRef.child(college).child(branch).child(year).child(section).child("exam");
                }