2017-08-22 8 views
0

私はAndroidベースのアプリケーションにFirebaseを使用しています。 firebaseのStorageセクションに画像をアップロードしてリストビューに表示しました。今、私はrecyclerviewの特定のイメージをクリックすると、次のアクティビティに移動し、次のアクティビティのimageviewでクリックしたイメージを表示します。特定のイメージを取得するには?助けてください。クリックに基づいてfirebaseからイメージをretieveする方法

firebaseのストレージに写真フォルダを作成しました。すべての画像はそのフォルダに保存されています。

public class MainActivity extends AppCompatActivity { 

    private static final String TAG = "MainActivity"; 

    public static final String ANONYMOUS = "anonymous"; 
    public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000; 
    public static final String FRIENDLY_MSG_LENGTH_KEY = "friendly_msg_length"; 

    public static final int RC_SIGN_IN = 1; 
    private static final int RC_PHOTO_PICKER = 2; 

    private ListView mMessageListView; 
    private MessageAdapter mMessageAdapter; 
    private ProgressBar mProgressBar; 
    private ImageButton mPhotoPickerButton; 
    private EditText mMessageEditText; 
    private Button mSendButton; 

    private String mUsername; 

    // Firebase instance variables 
    private FirebaseDatabase mFirebaseDatabase; 
    private DatabaseReference mMessagesDatabaseReference; 
    private ChildEventListener mChildEventListener; 
    private FirebaseAuth mFirebaseAuth; 
    private FirebaseAuth.AuthStateListener mAuthStateListener; 
    private FirebaseStorage mFirebaseStorage; 
    private StorageReference mChatPhotosStorageReference; 
    private FirebaseRemoteConfig mFirebaseRemoteConfig; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mUsername = ANONYMOUS; 

     // Initialize Firebase components 
     mFirebaseDatabase = FirebaseDatabase.getInstance(); 
     mFirebaseAuth = FirebaseAuth.getInstance(); 
     mFirebaseStorage = FirebaseStorage.getInstance(); 
     mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); 

     mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); 
     mChatPhotosStorageReference = mFirebaseStorage.getReference().child("photos"); 

     // Initialize references to views 
     mProgressBar = (ProgressBar) findViewById(R.id.progressBar); 
     mMessageListView = (ListView) findViewById(R.id.messageListView); 
     mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton); 
     mMessageEditText = (EditText) findViewById(R.id.messageEditText); 
     mSendButton = (Button) findViewById(R.id.sendButton); 

     // Initialize message ListView and its adapter 
     List<FriendlyMessage> friendlyMessages = new ArrayList<>(); 
     mMessageAdapter = new MessageAdapter(this, R.layout.item_message, friendlyMessages); 
     mMessageListView.setAdapter(mMessageAdapter); 

     // Initialize progress bar 
     mProgressBar.setVisibility(ProgressBar.INVISIBLE); 

     // ImagePickerButton shows an image picker to upload a image for a message 
     mPhotoPickerButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
       intent.setType("image/jpeg"); 
       intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); 
       startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER); 
      } 
     }); 

     // Enable Send button when there's text to send 
     mMessageEditText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
       if (charSequence.toString().trim().length() > 0) { 
        mSendButton.setEnabled(true); 
       } else { 
        mSendButton.setEnabled(false); 
       } 
      } 

      @Override 
      public void afterTextChanged(Editable editable) { 
      } 
     }); 
     mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT)}); 

     // Send button sends a message and clears the EditText 
     mSendButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null); 
       mMessagesDatabaseReference.push().setValue(friendlyMessage); 

       // Clear input box 
       mMessageEditText.setText(""); 
      } 
     }); 

     mAuthStateListener = new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
       FirebaseUser user = firebaseAuth.getCurrentUser(); 
       if (user != null) { 
        // User is signed in 
        onSignedInInitialize(user.getDisplayName()); 
       } else { 
        // User is signed out 
        onSignedOutCleanup(); 
        startActivityForResult(
          AuthUI.getInstance() 
            .createSignInIntentBuilder() 
            .setIsSmartLockEnabled(false) 
            .setProviders(
              AuthUI.EMAIL_PROVIDER, 
              AuthUI.GOOGLE_PROVIDER) 
            .build(), 
          RC_SIGN_IN); 
       } 
      } 
     }; 

     // Create Remote Config Setting to enable developer mode. 
     // Fetching configs from the server is normally limited to 5 requests per hour. 
     // Enabling developer mode allows many more requests to be made per hour, so developers 
     // can test different config values during development. 
     FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder() 
       .setDeveloperModeEnabled(BuildConfig.DEBUG) 
       .build(); 
     mFirebaseRemoteConfig.setConfigSettings(configSettings); 

     // Define default config values. Defaults are used when fetched config values are not 
     // available. Eg: if an error occurred fetching values from the server. 
     Map<String, Object> defaultConfigMap = new HashMap<>(); 
     defaultConfigMap.put(FRIENDLY_MSG_LENGTH_KEY, DEFAULT_MSG_LENGTH_LIMIT); 
     mFirebaseRemoteConfig.setDefaults(defaultConfigMap); 
     fetchConfig(); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == RC_SIGN_IN) { 
      if (resultCode == RESULT_OK) { 
       // Sign-in succeeded, set up the UI 
       Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show(); 
      } else if (resultCode == RESULT_CANCELED) { 
       // Sign in was canceled by the user, finish the activity 
       Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show(); 
       finish(); 
      } 
     } else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) { 
      Uri selectedImageUri = data.getData(); 

      // Get a reference to store file at chat_photos/<FILENAME> 
      StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment()); 

      // Upload file to Firebase Storage 
      photoRef.putFile(selectedImageUri) 
        .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() { 
         public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
          // When the image has successfully uploaded, we get its download URL 
          Uri downloadUrl = taskSnapshot.getDownloadUrl(); 

          // Set the download URL to the message box, so that the user can send it to the database 
          FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUrl.toString()); 
          mMessagesDatabaseReference.push().setValue(friendlyMessage); 
         } 
        }); 
     } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     mFirebaseAuth.addAuthStateListener(mAuthStateListener); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (mAuthStateListener != null) { 
      mFirebaseAuth.removeAuthStateListener(mAuthStateListener); 
     } 
     mMessageAdapter.clear(); 
     detachDatabaseReadListener(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.sign_out_menu: 
       AuthUI.getInstance().signOut(this); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    private void onSignedInInitialize(String username) { 
     mUsername = username; 
     attachDatabaseReadListener(); 
    } 

    private void onSignedOutCleanup() { 
     mUsername = ANONYMOUS; 
     mMessageAdapter.clear(); 
     detachDatabaseReadListener(); 
    } 

    private void attachDatabaseReadListener() { 
     if (mChildEventListener == null) { 
      mChildEventListener = new ChildEventListener() { 
       @Override 
       public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
        FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class); 
        mMessageAdapter.add(friendlyMessage); 
       } 

       public void onChildChanged(DataSnapshot dataSnapshot, String s) {} 
       public void onChildRemoved(DataSnapshot dataSnapshot) {} 
       public void onChildMoved(DataSnapshot dataSnapshot, String s) {} 
       public void onCancelled(DatabaseError databaseError) {} 
      }; 
      mMessagesDatabaseReference.addChildEventListener(mChildEventListener); 
     } 
    } 

    private void detachDatabaseReadListener() { 
     if (mChildEventListener != null) { 
      mMessagesDatabaseReference.removeEventListener(mChildEventListener); 
      mChildEventListener = null; 
     } 
    } 

    // Fetch the config to determine the allowed length of messages. 
    public void fetchConfig() { 
     long cacheExpiration = 3600; // 1 hour in seconds 
     // If developer mode is enabled reduce cacheExpiration to 0 so that each fetch goes to the 
     // server. This should not be used in release builds. 
     if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) { 
      cacheExpiration = 0; 
     } 
     mFirebaseRemoteConfig.fetch(cacheExpiration) 
       .addOnSuccessListener(new OnSuccessListener<Void>() { 
        @Override 
        public void onSuccess(Void aVoid) { 
         // Make the fetched config available 
         // via FirebaseRemoteConfig get<type> calls, e.g., getLong, getString. 
         mFirebaseRemoteConfig.activateFetched(); 

         // Update the EditText length limit with 
         // the newly retrieved values from Remote Config. 
         applyRetrievedLengthLimit(); 
        } 
       }) 
       .addOnFailureListener(new OnFailureListener() { 
        @Override 
        public void onFailure(@NonNull Exception e) { 
         // An error occurred when fetching the config. 
         Log.w(TAG, "Error fetching config", e); 

         // Update the EditText length limit with 
         // the newly retrieved values from Remote Config. 
         applyRetrievedLengthLimit(); 
        } 
       }); 
    } 

    /** 
    * Apply retrieved length limit to edit text field. This result may be fresh from the server or it may be from 
    * cached values. 
    */ 
    private void applyRetrievedLengthLimit() { 
     Long friendly_msg_length = mFirebaseRemoteConfig.getLong(FRIENDLY_MSG_LENGTH_KEY); 
     mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(friendly_msg_length.intValue())}); 
     Log.d(TAG, FRIENDLY_MSG_LENGTH_KEY + " = " + friendly_msg_length); 
    } 
} 
+0

最良の理解のためのあなたの完全なコードを投稿してください。ありがとう! – user7439667

+0

見てください。 –

答えて

0

recycleviewのためのonclickリスナーを実装し、

intent.putExtra("IMAGE_URL", "url of the image"); 
startActivity(intent); 

が第二の活動

String url = getIntent().getStringExtra("IMAGE_URL"); 
上のURLを取得するには、次のコードを使用して、次のように新しい活動に意図に画像のURLを渡します
+0

コードを追加しました。クリックした画像の画像URLをFirebaseストレージから取得する方法beacuseユーザーはfirebaseストレージから任意の画像を選択できます –

+0

MessageAdapterコードを投稿してください。 – RAM

0

イメージをアップロードすると、FirebaseによってダウンロードURLが作成されます。 あなたのコードから、Firebaseによって作成された画像ダウンロードURLがあることは明らかです。

を使用すると、ダウンロードURLを持っていけない場合でも、あなたはピカソのライブラリを使用することができますImageViewの中でそのイメージをロードするために

storageReferenceObj.getDownloadUrl(); 

を使用してそれを得ることができます。

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 

URLをダウンロードURLに置き換え、画像ビューを指定します。しかし、最初にピカソのライブラリを含めるために、この行をあなたのGradleファイルに追加してください。

compile 'com.squareup.picasso:picasso:2.5.2' 

さらに詳しい情報:http://square.github.io/picasso/

関連する問題