2012-04-03 7 views
0

Ich bin hier :) Ich versuche, ein Bild von der SD-Karte in eine Image auf diese Weise zu laden:einfache Änderung eines Image in Android

b_picture.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     Intent intent = new Intent(Intent.ACTION_PICK, 
           MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
     startActivityForResult(intent, 0); 
    } 
}); 

Hier versuche ich, um das Bild zu retrive und ändern Sie den Image:

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Bitmap bm = (Bitmap) data.getExtras().get("data"); 
    iv_picture.setImageBitmap(bm);  
} 

und ich bekomme diese von der logcat:

Failure delivering result ResultInfo {who=null, request=0, result=-1, 
      data=Intent { dat=content://media/external/images/media/2 

I ca Löse dieses Problem nicht. Kannst du mir helfen? Vielen Dank.

+0

mögen Sie, wie Sie Ihre Frage formatiert haben? –

+0

Zum zweiten Mal bearbeite ich Ihre Frage, warum Sie die alte gelöscht haben? –

Antwort

2

Die mit dem zurückgegebenen Intent verbundenen Daten sind keine Bitmap. Es ist ein URI, den Sie verwenden können, um im MediaStore ContentProvider nach dem gewünschten Bild zu suchen. :)

Sie finden ein meist funktionierendes Beispiel unter this question.

bearbeiten: So erweitern:

Wenn Sie gehen und fragen Sie die MediaStore für ein Bild, es wird Ihnen das aktuelle Bild nicht wiederkommen. Es gibt Ihnen einen URI zurück, mit dem Sie das Bild nachschlagen können. Die Art, wie Sie diesen URI in ein tatsächliches Bild übersetzen, lautet wie folgt:

Ihr URI ist content://media/external/images/media/2 wie die Fehlermeldung.

So erstellen wir im Grunde eine Abfrage und führen sie auf der MediaStoreContentProvider, die eine Datenbank von Bildern ist. Übergeben Sie die URI in dieser Funktion:

public Bitmap loadFullImage(Context context, Uri photoUri ) { 
    Cursor photoCursor = null; 

    try { 
     // Attempt to fetch asset filename for image 

     // DATA is the column name in the database for the filename of the image 
     String[] projection = { MediaStore.Images.Media.DATA }; 

     // use the URI you were given in order to look up the right image, 
     // and get a Cursor object that will iterate over the matching rows in the 
     // database. 
     photoCursor = context.getContentResolver().query(photoUri, 
               projection, null, null, null); 

     // since we only care about one image... 
     if (photoCursor != null && photoCursor.getCount() == 1) { 

      // go to the first row that was returned 
      photoCursor.moveToFirst(); 

      // get the string in the DATA column at that row 
      String photoFilePath = photoCursor.getString(
       photoCursor.getColumnIndex(MediaStore.Images.Media.DATA)); 

      // Load image from path 
      return BitmapFactory.decodeFile(photoFilePath, null); 
     } 
    } finally { 

     // close up the cursor 
     if (photoCursor != null) { 
      photoCursor.close(); 
     } 
    } 

    return null; 
} 
+0

pleeasss .... mehr Informationen, ich sah in dem Beispiel du hast mir gegeben und ich bekomme es nicht :( – roiberg

+0

Okay. Lassen Sie mich überarbeiten. –

+0

Wenn es funktioniert, vergessen Sie nicht, die Antwort zu akzeptieren. :) –

0

// Pfad ab: /sdcard/myimage.png

String imageInSD = "Your image path here"; 
BitmapFactory.Options bOptions = new BitmapFactory.Options(); 
bOptions.inTempStorage = new byte[16*1024]; 

bitmap = BitmapFactory.decodeFile(imageInSD,bOptions); 

imageView.setImageBitmap(bitmap);