Is there any other way to take photos from Firebase? I used Picasso but photos don’t show on activity

  android, database, firebase, photo, picasso

I used Picasso but photos don’t show. But text is showing in activity.
Here the codes

Upload Activity

In this activity, I am uploading photos to Firebase.

public class UploadActivity extends AppCompatActivity {

    EditText description;
    ImageView imageView;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference myRef;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private StorageReference mStorageRef;
    Uri selected;


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

        description = findViewById(R.id.editTextPostDescription);
        imageView = findViewById(R.id.imageView);
        firebaseDatabase = FirebaseDatabase.getInstance();
        myRef = firebaseDatabase.getReference();
        mAuth = FirebaseAuth.getInstance();
        mStorageRef = FirebaseStorage.getInstance().getReference();


    }

    public void upload(View view){

        UUID uuid = UUID.randomUUID();
        String imageName = "images/" + uuid + ".jpg";
        StorageReference storageReference = mStorageRef.child(imageName);

        storageReference.putFile(selected).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                String url = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();

                FirebaseUser user = mAuth.getCurrentUser();
                String userEmail = user.getEmail().toString();
                String desc = description.getText().toString();

                UUID uuid1 = UUID.randomUUID();
                String uuidString = uuid1.toString();

                myRef.child("Posts").child(uuidString).child("useremail").setValue(userEmail);
                myRef.child("Posts").child(uuidString).child("description").setValue(desc);
                myRef.child("Posts").child(uuidString).child("downloadurl").setValue(url);

                Toast.makeText(UploadActivity.this, "Post shared!", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getApplicationContext(),FeedActivity.class);
                startActivity(intent);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(UploadActivity.this, e.getLocalizedMessage().toString(), Toast.LENGTH_LONG).show();
            }
        });

    }


    public void chooseImage(View view){

        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,
                    new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},1);
        }
        else{
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent,2);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if(requestCode == 1){
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent,2);
            }
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        if(requestCode == 2 && resultCode == RESULT_OK && data != null){
            selected = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),selected);
                imageView.setImageBitmap(bitmap);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

PostClass

In postclass, I tried to create arraylists and taking data from database.

public class PostClass extends ArrayAdapter<String> {

    private final ArrayList<String> useremails;
    private final ArrayList<String> userImage;
    private final ArrayList<String> userDescription;
    private final Activity context;


    public PostClass(ArrayList<String> useremails, ArrayList<String> userImage, ArrayList<String> userDescription, Activity context) {
        super(context,R.layout.new_custom_view,useremails);
        this.useremails = useremails;
        this.userImage = userImage;
        this.userDescription = userDescription;
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        
        LayoutInflater layoutInflater = context.getLayoutInflater();

        View customeView = layoutInflater.inflate(R.layout.new_custom_view,null, true);
        
        TextView userName = customeView.findViewById(R.id.textView);
        TextView userDesc = customeView.findViewById(R.id.textView2);
        ImageView imageView = customeView.findViewById(R.id.imageView3);
        
        userName.setText(useremails.get(position));
        userDesc.setText(userDescription.get(position));
        Picasso.get().load(userImage.get(position)).into(imageView);
        
        return customeView;
    }
}

Feed Activity

In feed activity, I tried to show useremail, photos and descriptions on this activity. Useremail and descriptions are coming but photos are don’t come. I also used Gridle but didn’t work.

public class FeedActivity extends AppCompatActivity {

    ArrayList<String> useremailfromDB;
    ArrayList<String> userdescfromDB;
    ArrayList<String> userimagesfromDB;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference myRef;
    PostClass adapter;
    ListView listView;


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

        listView = findViewById(R.id.listView);

        useremailfromDB = new ArrayList<String>();
        userdescfromDB = new ArrayList<String>();
        userimagesfromDB = new ArrayList<String>();
        firebaseDatabase = FirebaseDatabase.getInstance();
        myRef = firebaseDatabase.getReference();

        getDataFromDB();

        adapter = new PostClass(useremailfromDB,userimagesfromDB,userdescfromDB,this);

        listView.setAdapter(adapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.add_post,menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        if(item.getItemId() == R.id.add_post ){
            Intent intent = new Intent(getApplicationContext(),UploadActivity.class);
            startActivity(intent);
        }

        return super.onOptionsItemSelected(item);
    }

    protected void getDataFromDB(){

        DatabaseReference databaseReference = firebaseDatabase.getReference("Posts");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for(DataSnapshot ds : snapshot.getChildren()){

                    HashMap<String,String> hashMap = (HashMap<String, String>) ds.getValue();
                    useremailfromDB.add(hashMap.get("useremail"));
                    userimagesfromDB.add(hashMap.get("downloadurl"));
                    userdescfromDB.add(hashMap.get("description"));
                    adapter.notifyDataSetChanged();
                    
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }
}

Source: Android Questions

LEAVE A COMMENT