2017-08-07 7 views
0

私は外部記憶装置を介してfirebaseストレージに写真をアップロードしようとしています。私のアプリケーションでは、試験と写真の2種類のタイムテーブルがあります。 upload_timetableまたはupload_examの間にstringExtraを渡す方法です。onActivityResult()は、upload_timetableまたはupload_examから作成することができます。onActivityResult()でstringExtraを取得する方法をインテントに渡しますか?

私はこの方法を実装しました。しかし、私はupload_timetableまたはupload_exam をクリックした場合、私はいつもトーストにエラー私は

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

答えて

0
を渡された文字列を取得する文字列

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

コードを渡すmessage.Help私ください

コードを取得します

startActivityForResultに1つのリクエストコード(GALLERY_INTENT)を使用する代わりに、2つの異なるコードを作成して、onActivityResultを取得すれば確認できます。

2要求コード受験

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 
      } 

     } 
    } 
については

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

を作成します。

+0

両方のアクションはギャラリーインテントタイプですが、他のタイプに置き換えることはできません。 –

+0

私は私の答えを更新しました。コードの上記の –

+0

が働いた。そして値を変更するだけでとても簡単でした。ありがとう –

0

それは正しい方法で、あなたのコード内で使用すると、キー「sExamType」を定義する場所最初はそれが

String sExamType="SExamTypeKey"; 

のようなものである必要があり、その後、あなたにonActivityResultあなたは

String sTypes = data.getStringExtra(sExamType); 

をworte、それは大丈夫です余分な文字列を取得するには、しかし、同じ場合は両方の "タイムテーブル"をチェックしてください "試験"です。

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"); 
       } 
関連する問題