0

I want my newfeed.java tab to allow users to choose an image from gallery and > upload it to the firebase storage. Also, how to retrieve these images and >display in a listview ?

Newfeed.java

    import static android.app.Activity.RESULT_OK;

    public class NewfeedTab extends Fragment {

private static final int PICK_IMAGE_REQUEST = 234;

private StorageReference mStorageRef;

private EditText story;
private ImageButton choose;
private ImageButton upload;
private Uri filePath;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.news_feed, container, false);

    mStorageRef = FirebaseStorage.getInstance().getReference();
    story = (EditText)rootView.findViewById(R.id.et_story);
    choose = (ImageButton)rootView.findViewById(R.id.imageButton_choose);
    upload = (ImageButton)rootView.findViewById(R.id.imageButton_upload);
    choose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

        }


    });
    upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(filePath != null){
                final ProgressDialog progressDialog = new ProgressDialog(getContext());
                progressDialog.setTitle("Uploading");
                progressDialog.show();

                StorageReference picsRef = mStorageRef.child("images/pic.jpg");
                picsRef.putFile(filePath)
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                progressDialog.dismiss();
                                Toast.makeText(getContext(),"Story posted",Toast.LENGTH_SHORT);
                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                progressDialog.dismiss();
                                Toast.makeText(getContext(),"Story couldn't be posted",Toast.LENGTH_SHORT);
                            }
                        })
                        .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        //calculating progress percentage
                        double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();

                        //displaying percentage in progress dialog
                        progressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
                    }
                });
            }
        }
    });
    return rootView;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        filePath = data.getData();

    }
}

}

The app closes after selecting the image from gallery. NewfeedTab.java is a fragment of tabbed activity.

newsfeed.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.seven.pprs.bloodlink.TabActivity$PlaceholderFragment">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:ems="10"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:id="@+id/et_story"
    android:layout_toStartOf="@+id/imageButton_choose"
    android:hint="Share your stories here" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/ic_file_upload_black_24dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/imageButton_upload" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:dividerHeight="1dp"
    android:divider="@android:color/holo_red_dark"
    android:id="@+id/list_of_posts"
    android:layout_marginBottom="16dp"
    android:layout_below="@+id/et_story" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/ic_add_a_photo_black_24dp"
    android:layout_alignBottom="@+id/imageButton_upload"
    android:layout_toStartOf="@+id/imageButton_upload"
    android:id="@+id/imageButton_choose" />

raviw3
  • 23
  • 5

1 Answers1

0

Do you have read permission in your AndroidManifest.xml:

           <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Chester Cobus
  • 701
  • 4
  • 12
  • Thank you. I added it now and the activity resumes after selecting an image. But when I click on upload button, it gives an error as `W/DynamiteModule: Local module descriptor class for com.google.android.gms.firebasestorage not found.` – raviw3 Mar 23 '17 at 04:06
  • Specify your storage reference like this (get the URL from Firebase console) : StorageReference mStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl("gs://{nameOfStorage}.appspot.com"); – Chester Cobus Mar 23 '17 at 07:41
  • Please could you provide me with the gradle file so I can see your dependencies – Chester Cobus Mar 23 '17 at 07:55
  • the activity resumes only in the emulator, I still get the same problem when I run it on my device. – raviw3 Mar 23 '17 at 15:03
  • https://docs.google.com/document/d/1VjFKnfiqJh447sjjvatcB3KujRi3jwST4PoqTY_xzm0/edit?usp=sharing you can see gradle file here – raviw3 Mar 23 '17 at 15:18