2014-01-15 6 views
6

Ich habe eine App, die eine Schaltfläche hat, um ein Foto aus Ihrer Galerie auszuwählen, und es funktioniert gut und nach der Auswahl des Bildes kam meine App zurück zu der Aktivität und zeigt die Bild in einer Bildansicht.Ein Foto aus der Galerie auswählen und in einer Bildansicht anzeigen

Alles funktioniert gut, aber manchmal, wenn ich bestimmte Bilder auswählen, wird die Vorschau nicht angezeigt. Ich habe auch versucht, noch das Bild zu komprimieren seine Arbeit nicht

Mein Code unten ist .. In onCreate()

galeryBtn=(Button)findViewById(R.id.buttonGallery); 
galeryBtn.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(i, RESULT_LOAD_IMAGE); 

    } 
}); 

In onActivityResult (int requestCode, int Result, Intent-Daten)

Antwort

5

Ich laufe in ähnliche Probleme wie immer Cursor uri von Ressourcen, offenen Stream, Set Bitmap usw. Und es hat Bugs die ganze Zeit.

Also habe ich Bibliotheken durchsucht und Bild-Chooser-Library-Bibliothek gefunden.

Ich wette, Sie möchten, um dieses Projekt versuchen, von image-chooser-library

Es ist sehr einfach zu bedienen und löst alle diese Nitty Gritty Probleme für Sie, wie Bilder von picasa usw.

Hoffentlich ist es nützlich, Sie.

+0

Vielen Dank, es funktioniert immer noch ich muss in verschiedenen Telefonen testen – Biplab

+0

Ich habe getestet, aber wenn ich einige Bilder aus dem Telefon wählen toest "Datei nicht gefunden" – Biplab

+0

Haben Sie Bilder von Highlight ausgewählt? Ist das so, schau dir den Zweig dev_get_content an und schau, ob es funktioniert. –

3

Die Art, wie Sie versuchen, eine Bitmap in onActivityResult() zu laden, ist nicht absolut richtig. Manchmal können Sie kein Bild öffnen und Ihre Anwendung kann abstürzen. Sie würden besser Code verwenden:

Uri imageUri = data.getData(); 
InputStream imageStream = null; 
try { 
    imageStream = getContentResolver().openInputStream(imageUri); 
    ImageView imageView = (ImageView) findViewById(R.id.imgView); 
    imageView.setImageBitmap(BitmapFactory.decodeStream(imageStream)); 
} catch (FileNotFoundException e) { 
    // Handle the error 
} finally { 
    if (imageStream != null) { 
     try { 
      imageStream.close(); 
     } catch (IOException e) { 
      // Ignore the exception 
     } 
    } 
} 
5

wie Try this

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     super.onActivityResult(requestCode, resultCode, intent); 

     switch (requestCode) { 
       case RESULT_LOAD_IMAGE: 
      if (resultCode == Activity.RESULT_OK) { 

       Uri selectedImage = intent.getData(); 
       try { 
        Bitmap bitmapImage =decodeBitmap(selectedImage); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
           // Show the Selected Image on ImageView 
        ImageView imageView = (ImageView) findViewById(R.id.imgView); 
        imageView.setImageBitmap(bitmapImage); 

      } 

Und

public Bitmap decodeBitmap(Uri selectedImage) throws FileNotFoundException { 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o); 

     final int REQUIRED_SIZE = 100; 

     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) { 
       break; 
      } 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2); 
    } 
+0

Ihr Code funktioniert gut.Aber wie erhöht man die Höhe und die Höhe des ImageView? – reegan29

+1

funktioniert wie Charme :) –

-1
final int SELECT_PICTURE = 1; 
public void openGallary(){ 
Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent,"SelectPicture"),SELECT_PICTURE); 
} 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
     case SELECT_PICTURE: 
    Uri selectedImageUri = data.getData(); 
    imgPerview.setImageURI(selectedImageUri);}} 
0

Fügen Sie dies nach imageView.setImageBitmap (BitmapFactory.decodeFile (picturePath));

imageView.setImageURI(selectedImage); 

Es funktioniert für mich.