ImageView does not display image taken from phone camera or photo gallery

NGSBNC

I am currently designing an android app that requires at a certain point for the user to select a picture either from the camera or from the phone's gallery. After that I need to display said picture in the activity's image view.

I think the code section that gets the picture is ok, as I can effectively pick the photo from both options. But after that, besides having no picture shown, the src logo of the image view dissapears. I do not understand if I'm doing anything wrong or if there is something missing here, as it is a non-fatal error and the app continues to perform.

So here is my XML layout of the activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat 
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"
style="@style/Widget.Design.CoordinatorLayout"
android:orientation="vertical"

tools:context="pt.unl.fct.di.www.myapplication.ReportActivity">

<ScrollView
    android:id="@+id/report_form"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/submit_report_form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

        <TextView
            android:id="@+id/type_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="30dp"
            android:text="@string/select_report_type"
            android:visibility="visible" />
        <Spinner
            android:id="@+id/report_type_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:layout_marginBottom="30dp"/>


        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/description"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Description"
                android:inputType="textMultiLine"
                android:selectAllOnFocus="false"
                android:layout_marginBottom="30dp"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Location"
                android:layout_marginBottom="30dp"/>

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Map"
                android:layout_marginBottom="30dp"/>
        </android.support.design.widget.TextInputLayout>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@android:drawable/ic_menu_camera"
            android:adjustViewBounds="true"
             />

        <Button
            android:id="@+id/camera_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add Picture" />

        <Button
            android:id="@+id/submit_report_button"
            style="?android:textAppearanceSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/action_submit_report"
            android:textStyle="bold"
            android:layout_weight="0.68" />


    </LinearLayout>
</ScrollView>

And the Activity's java(important stuff):

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_report);
    Spinner spinner = (Spinner) findViewById(R.id.report_type_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.report_types, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
    spinner.setAdapter(adapter);
    mReportType = spinner.getItemAtPosition(0).toString();
    spinner.setOnItemSelectedListener(this);
    mDescription = (EditText)findViewById(R.id.description);
    mImageLocationView =(ImageView) findViewById(R.id.imageView);
    Button cameraButton = (Button)findViewById(R.id.camera_button);
    cameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle(R.string.choose_photo_from)
                    .setItems(R.array.picture_types, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // The 'which' argument contains the index position
                            // of the selected item
                            switch(which){
                                case 0: //fotografia
                                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    startActivityForResult(takePicture, 0);
                                    onPause();
                                    //zero can be replaced with any action code
                                    break;
                                case 1:
                                    //galeria
                                    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                                    startActivityForResult(pickPhoto , 1);
                                    //one can be replaced with any action code
                                    break;
                            }


                        }
                    });
            builder.create().show();
        }
    });


}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    BitmapFactory.Options o = new BitmapFactory.Options();
    mImageLocationView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    switch(requestCode) {
        case 0:
            if(resultCode == RESULT_OK){
                Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
                mImageLocationView.setImageBitmap(photo);

            }

            break;
        case 1:
            if(resultCode == RESULT_OK){
                Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
                mImageLocationView.setImageBitmap(photo);
            }
            break;
    }

}

I must say, this is my very first android project so I am kind of overwhelmed by so much information and APIS, and also doing a lot of experimenting.

yeaaqbi

Simply you can add the following code in `

if(resultCode == RESULT_OK){   
    // the new code
     Uri imageUri = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getActivity().getContentResolver().query(imageUri, filePath, null, null, null);
            cursor.moveToFirst();
            String path = cursor.getString(cursor.getColumnIndex(filePath[0]));
            cursor.close();

            //refresh Image view
            updateImageView(path);
        }
// the update image method
    public void updateImageView(String completePath) {
         Picasso picasoo = Picasso.with(getActivity());
          String path = "file://" + completePath;
          Long tsLong = System.currentTimeMillis();
         String ts = tsLong.toString();
          picasoo.load(path+"?time="+ts).fit().centerCrop().networkPolicy(OFFLINE).into(imgUserProfile);

}` Picasso is an image management library that make your life easy with lazy loading the image.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I want to store image in sqlite database that is taken from gallery and camera android

From Dev

How to open photo gallery by clicking a button from a camera overlay?

From Dev

Trying to display image taken from camera intent

From Dev

Picking a photo from gallery and show in a image view

From Dev

Set image taken from camera into an ImageView

From Dev

Resize image taken from gallery or camera, before being uploaded

From Dev

take photo with camera or choose from gallery popover using phonegap

From Dev

Set ImageView with selected Image from Camera or Gallery

From Dev

Resolution of image from camera to Imageview

From Dev

taken from android camera image original resolution

From Dev

Setting the image taken from the gallery to the UIImageView

From Dev

Saving image taken from camera into INTERNAL storage

From Dev

image load in imageview from camera or gallery

From Dev

Is it possible to check if an image is taken by camera or uploaded from photo library by JavaScript?

From Dev

Display last captured photo in a imageView? (Camera)

From Dev

I want to read exif info in image in android. I can read exif from image in gallery but i cannot read exif photo taken from the camera

From Dev

Is it possible to display an image over a photo taken in app

From Dev

Read thumbnail image taken from phone camera

From Dev

select image from phone gallery

From Dev

android display image in imageview from camera

From Dev

Pick an random image from photo gallery

From Dev

take photo with camera or choose from gallery popover using phonegap

From Dev

How can i set an image taken from the Camera intent into a ImageView?

From Dev

Resolution of image from camera to Imageview

From Dev

Android : Use Camera to display image to ImageView and Save to Gallery

From Dev

Android - Saving photo taken from Camera , Out of memory

From Dev

Save Image (took with Camera Intent) from ImageView to device (Gallery/SdCard)

From Dev

How to upload particular image on particular imageView click from gallery and camera in android

From Dev

image taken from gallery is unable to set in image view

Related Related

  1. 1

    I want to store image in sqlite database that is taken from gallery and camera android

  2. 2

    How to open photo gallery by clicking a button from a camera overlay?

  3. 3

    Trying to display image taken from camera intent

  4. 4

    Picking a photo from gallery and show in a image view

  5. 5

    Set image taken from camera into an ImageView

  6. 6

    Resize image taken from gallery or camera, before being uploaded

  7. 7

    take photo with camera or choose from gallery popover using phonegap

  8. 8

    Set ImageView with selected Image from Camera or Gallery

  9. 9

    Resolution of image from camera to Imageview

  10. 10

    taken from android camera image original resolution

  11. 11

    Setting the image taken from the gallery to the UIImageView

  12. 12

    Saving image taken from camera into INTERNAL storage

  13. 13

    image load in imageview from camera or gallery

  14. 14

    Is it possible to check if an image is taken by camera or uploaded from photo library by JavaScript?

  15. 15

    Display last captured photo in a imageView? (Camera)

  16. 16

    I want to read exif info in image in android. I can read exif from image in gallery but i cannot read exif photo taken from the camera

  17. 17

    Is it possible to display an image over a photo taken in app

  18. 18

    Read thumbnail image taken from phone camera

  19. 19

    select image from phone gallery

  20. 20

    android display image in imageview from camera

  21. 21

    Pick an random image from photo gallery

  22. 22

    take photo with camera or choose from gallery popover using phonegap

  23. 23

    How can i set an image taken from the Camera intent into a ImageView?

  24. 24

    Resolution of image from camera to Imageview

  25. 25

    Android : Use Camera to display image to ImageView and Save to Gallery

  26. 26

    Android - Saving photo taken from Camera , Out of memory

  27. 27

    Save Image (took with Camera Intent) from ImageView to device (Gallery/SdCard)

  28. 28

    How to upload particular image on particular imageView click from gallery and camera in android

  29. 29

    image taken from gallery is unable to set in image view

HotTag

Archive