2

strong textI am trying to load a picture from the gallery and display it in an image view, PNG pictures are being displayed fine but the JPG images are not displaying at all and I have no idea why

here is the code:

public void openGa(View view){
        //create a ga||ery intent and set the action to pick an object
      Intent galleryIntent = new Intent(Intent.ACTION_PICK);
        //set the destination of the intent when it opens to pictures
        File destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        //get the path as a string to cast to URI
        String path = destination.getAbsolutePath();
        //cast the path to uri
        Uri pathUri = Uri.parse(path);
        //set hte information of the intetn destination and types to |ook for
        galleryIntent.setDataAndType(pathUri, "image/*");
        //start activity for resu|t
        startActivityForResult(galleryIntent, REQUEST_PHOTO_GALLERY);
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == REQUEST_PHOTO_GALLERY && resultCode == RESULT_OK){
            //get the image as uri
            Uri imageUri = data.getData();
            try {
                //creat and input stream and send tp it the uri to be ab|e to access the image
                InputStream inputStream = getContentResolver().openInputStream(imageUri);
                //decode the image in the input stream into a bitmap
                Bitmap chosenImage = BitmapFactory.decodeStream(inputStream);

                ImageView i = (ImageView)findViewById(R.id.imageView);
                i.setImageBitmap(chosenImage);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

this is my xml

 TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

           <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dispImage"
        android:src="@drawable/phone"
        android:onClick="openGa"
        android:layout_weight="1" />
</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView" />
</TableRow>
</TableLayout>
Euthalia
  • 21
  • 1
  • 4
  • are you sure that the ImageView is visible (has non-zero size)? Can you please put your layout file? Moreover, have you checked that the code for image loading is actually being executed? BTW, for image loading I'd suggest using some existing library like Picasso, for instance. – Chaosit Jul 11 '16 at 13:34
  • Try this. http://stackoverflow.com/a/11004897/5723796 – Sagar Thakarar Jul 11 '16 at 13:35
  • Can you please confirm that the JPEG images are actually in the JPEG format and not just the file extension changed from PNG to JPEG. – Ruchira Randana Jul 11 '16 at 13:37
  • the code is working PNG images are being loaded into the image view normally but JPEG pictures are not being displayed, i'm not familiar with this Picasso thingie :p i'm gonna research it thank you ! – Euthalia Jul 11 '16 at 13:38
  • well the pictures are the ones in the gallery, most of them are taken by the camera so yeah to be precise they are in jpg format – Euthalia Jul 11 '16 at 13:42
  • @Euthalia if u wan't to rescale the image use this http://stackoverflow.com/questions/43487288/android-how-to-convert-the-compressed-images-to-string-with-minimum-size – KJEjava48 Apr 19 '17 at 07:48

1 Answers1

2

The jpg's are too big to display in an ImageView. The BitmapFactory is unable to make a Bitmap for them by lack of memory. See for yourself that chosenImage becomes null.

Rescale the bitmap while loading.

Try with a small .jpg to see that size matters.

greenapps
  • 11,154
  • 2
  • 16
  • 19