2013-06-04 12 views
7

Ich weiß, wie ein Foto aus der Galerie wählt Miniatur aus android Galerie

Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(gallery, PHOTO_REQUEST_CODE); 

in android

zu bekommen, aber wie würde ich ein Thumbnail speziell wählen?

GRUND FÜR PRÄMIEN:

ich bereits beide Lösungen bei Get thumbnail Uri/path of the image stored in sd card + android versucht haben. Sie arbeiten nicht für mich. Ich weiß nicht, wie selectedImageUri, erhalten, die vom Typ ist long, von data in

onActivityResult(int requestCode, int resultCode, Intent data) 
+1

Mögliches Duplikat [Get Thumbnail Uri/Pfad des Bildes in SD-Karte gespeichert + Android] (http://stackoverflow.com/questions/5548645/get-thumbnail-uri-path- von-the-image-gespeichert-in-sd-karte-android) –

+0

@MiroMarkarian danke für den link. Aber ich bin immer Fehler, wenn ich 'Bitmap Bitmap verwenden = MediaStore.Images.Thumbnails.getThumbnail ( \t \t \t \t getContentResolver(), data.getData(), \t \t \t \t MediaStore.Images.Thumbnails.MINI_KIND, \t \t \t \t (BitmapFactory.Options) null); '. Wie würde ich 'data.getData()' in eine ID ändern, wie in der Antwort vorgeschlagen? –

+0

Versuchen Sie 'Cursors' zu verwenden. Es ist die zweite Lösung, die der Typ dort vorgeschlagen hat und es wurde berichtet, dass es besser funktioniert als die Option "Bitmap" –

Antwort

0
String fn = ...; // file name 
ContentResolver cr = ctx.getContentResolver(); 
Cursor c = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
      new String[]{ 
       BaseColumns._ID 
      }, MediaColumns.DATA + "=?", new String[]{ fn }, null); 
    if(c!=null) { 
     try{ 
      if(c.moveToNext()) { 
       long id = c.getLong(0); 
       Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null); 
      } 
     }finally{ 
      c.close(); 
     } 
    } 
0

Wenn Sie ihre Cursor in der Hand haben, können Sie deren ID erhalten, wie,

int id = cursor.getInt(cursor 
        .getColumnIndex(MediaStore.MediaColumns._ID)); 

Reffer der folgende Code

Cursor cursor = context.getContentResolver().query(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
       new String[] { MediaStore.Images.Media._ID }, 
       MediaStore.Images.Media.DATA + "=? ", 
       new String[] { filePath }, null); 

     if (cursor != null && cursor.moveToFirst()) { 
      int id = cursor.getInt(cursor 
        .getColumnIndex(MediaStore.MediaColumns._ID)); 
      Uri baseUri = Uri.parse("content://media/external/images/media"); 
      return Uri.withAppendedPath(baseUri, "" + id); 

Also, für Thumbnail,

Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(cursor, id, MediaStore.Images.Thumbnails.MINI_KIND, null); 
0

Hey, wenn alles andere nicht für dich funktioniert hier ist eine einfache Möglichkeit, dein eigenes Thumbnail zu erstellen, wenn du die Bitmap hast. Wenn Sie nicht wissen, wie das Bitmap aus der Uri zu laden:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri); 

Hier ist der Code ist ein schönes formatiert Thumbnail zu machen:

 final int THUMBNAIL_HEIGHT = 75;//48 
     final int THUMBNAIL_WIDTH = 75;//66 
     Float width = new Float(bitmap.getWidth()); 
     Float height = new Float(bitmap.getHeight()); 
     Float ratio = width/height; 
     bitmap = Bitmap.createScaledBitmap(bitmap, (int)(THUMBNAIL_HEIGHT*ratio), THUMBNAIL_HEIGHT, false); 

     int padding = (THUMBNAIL_WIDTH - bitmap.getWidth())/2; 
     image.setPadding(padding, padding, padding, padding); 
     image.setBackgroundColor(0); 
     image.setImageBitmap(bitmap); 

In diesem Code „Bild“ ist die Variable für das ImageView. Ich hoffe, das hilft einigen: D

Verwandte Themen