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

时间:2017-08-07 10:12:30

标签: android firebase android-intent onactivityresult

我正在尝试通过外部存储将照片上传到firebase存储。有两种类型的照片,一种是考试,另一种是我的应用程序中的时间表。如何在upload_timetable或upload_exam中传递stringExtra,以便{i}我可以知道它来自upload_timetable或upload_exam。

我已经实现了这种方式。但是,如果我点击upload_timetable或upload_exam 我总是在toast message中收到错误。请帮助我

我传递字符串的代码

onActivityResult()

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

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);
        }

2 个答案:

答案 0 :(得分:0)

只为<DataGridComboBoxColumn Header="Status" SelectedValueBinding="{Binding Status}" SelectedValuePath="Tag" xmlns:s="clr-namespace:System;assembly=mscorlib"> <DataGridComboBoxColumn.ItemsSource> <x:Array Type="{x:Type Image}"> <Image Source="1.png"> <Image.Tag> <s:Int32>1</s:Int32> </Image.Tag> </Image> <Image Source="2.png"> <Image.Tag> <s:Int32>2</s:Int32> </Image.Tag> </Image> </x:Array> </DataGridComboBoxColumn.ItemsSource> </DataGridComboBoxColumn> 创建两个请求代码(GALLERY_INTENT),创建2个不同的代码,以便在startActivityForResult中获得该代码并进行验证。

创建2个请求代码

onActivityResult

征集时间表

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

考试

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

onActivityResult代码

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

答案 1 :(得分:0)

首先在代码中定义键&#34; sExamType&#34;它应该像

String sExamType="SExamTypeKey";

然后在你的onActivityResult中你

String sTypes = data.getStringExtra(sExamType);

这是正确的,它是检索额外字符串的正确方法,但是如果两个检查的时间表都是相同的,则时间表和#34;不是&#34;考试&#34;。

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");
                }