2016-07-11 16 views
1

starkes textIch versuche ein Bild aus der Galerie zu laden und es in einer Bildansicht anzuzeigen, PNG Bilder werden gut angezeigt, aber die JPG Bilder werden nicht angezeigt und ich habe keine Ahnung warumImageView zeigt PNG aber nicht JPEG an

hier ist der 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(); 
      } 
     } 
    } 

das ist mein 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> 
+0

Sind Sie sicher, dass die ImageView sichtbar ist (hat eine Größe ungleich Null)? Können Sie bitte Ihre Layoutdatei einfügen? Haben Sie außerdem überprüft, dass der Code zum Laden des Bildes tatsächlich ausgeführt wird? Übrigens würde ich zum Laden von Bildern vorschlagen, eine vorhandene Bibliothek wie Picasso zu verwenden. – Chaosit

+0

Versuchen Sie dies. http://StackOverflow.com/a/11004897/5723796 –

+0

Können Sie bitte bestätigen, dass die JPEG-Bilder tatsächlich im JPEG-Format sind und nicht nur die Dateiendung von PNG in JPEG geändert wurde. –

Antwort

1

die jpg sind zu groß, in einem Image anzuzeigen. Die BitmapFactory kann aus Mangel an Speicher keine Bitmap für sie erstellen. Sehen Sie selbst, dass chosenImage null wird.

Skalieren Sie die Bitmap beim Laden neu.

Versuchen Sie mit einem kleinen .jpg, um zu sehen, dass Größe wichtig ist.

+0

danke, wie kann ich sie neu skalieren? – Euthalia

+0

Der Code dafür wurde viele Male auf dieser Seite veröffentlicht. – greenapps

Verwandte Themen