2012-03-09 27 views
10

ja ściągam URI obraz z galerii za pomocąUri wrócił po ACTION_GET_CONTENT z galerii nie pracuje w setImageURI() z ImageView

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent, "Choose Picture"), requestCode); 

i stara się wyświetlić obraz przez

imageView.setImageURI(uri); 

tutaj , uri to Uri obrazu otrzymanego w onActivityResult przez intent.getData().

ale obraz nie jest wyświetlany. Ponadto dla pliku

plik.exists() zwraca wartość false.

+0

czy sprawdzasz wartość uri .. log i sprawdź .. wklej tutaj URI – Ronnie

Odpowiedz

22

Problem polega na tym, że dostajesz Uri, ale z tego musisz utworzyć mapę bitową, aby pokazać w swoim Widoku. Istnieją różne mechanizmy, aby zrobić ten sam wśród nich jest ten kod.

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent, "Choose Picture"), 1); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if(resultCode==RESULT_CANCELED) 
    { 
     // action cancelled 
    } 
    if(resultCode==RESULT_OK) 
    { 
     Uri selectedimg = data.getData(); 
     imageView.setImageBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedimg)); 
    } 
} 
+0

jego działa dla mnie .. dzięki :) –

0

Uruchom Chooser Galeria zdjęć

Intent intent = new Intent(); 
// Show only images, no videos or anything else 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
// Always show the chooser (if there are multiple options available) 
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); 

PICK_IMAGE_REQUEST jest kod żądania zdefiniowana jako zmiennej instancji.

private int PICK_IMAGE_REQUEST = 1; 

Pokaż na wybrany obraz w działalności/Fragment

@Override 
protected 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) { 

     Uri uri = data.getData(); 

     try { 
      Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); 
      // Log.d(TAG, String.valueOf(bitmap)); 

      ImageView imageView = (ImageView) findViewById(R.id.imageView); 
      imageView.setImageBitmap(bitmap); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

Twój układ będzie musiał mieć ImageView takiego:

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView" />