2013-06-04 29 views
7

tôi biết làm thế nào để có được một bức ảnh từ bộ sưu tập trong androidchọn thumbnail từ android gallery

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

Nhưng làm thế nào sẽ Tôi đặc biệt chọn một hình thu nhỏ?

LÝ DO CHO BOUNTY:

Tôi đã thử cả hai giải pháp tại Get thumbnail Uri/path of the image stored in sd card + android. Họ không làm việc cho tôi. Tôi không biết làm thế nào để có được selectedImageUri, mà là loại long, từ data trong

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

Bản sao có thể có của [Nhận hình thu nhỏ Uri/đường dẫn của hình ảnh được lưu trữ trong thẻ sd + android] (http://stackoverflow.com/questions/5548645/get-thumbnail-uri-path- of-the-image-được lưu trữ-in-sd-card-android) –

+0

@MiroMarkarian cảm ơn vì liên kết. Nhưng tôi nhận được lỗi khi tôi sử dụng 'Bitmap bitmap = 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); '. Làm cách nào để tôi thay đổi 'data.getData()' thành một id như được đề xuất trong câu trả lời? –

+0

Hãy thử sử dụng 'Con trỏ'. Đó là giải pháp thứ hai mà anh chàng đề xuất ở trên và được báo cáo rằng nó hoạt động tốt hơn tùy chọn 'Bitmap' –

Trả lời

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

Nếu bạn có con trỏ của nó trong tay, bạn có thể nhận được ID của nó như,

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

Reffer đoạn mã sau

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); 

vì vậy, ví thumbnail,

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

Vì vậy, nếu mọi thứ khác không hoạt động cho bạn ở đây là cách dễ dàng để tạo hình thu nhỏ của riêng bạn nếu bạn có Bitmap. Nếu bạn không biết làm thế nào để nạp Bitmap từ Uri:

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

Đây là đoạn mã để tạo ra một hình ảnh thu nhỏ được định dạng đẹp:

 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); 

Trong mã "hình ảnh" này là biến cho ImageView. Tôi hy vọng điều này sẽ giúp một số: D

Các vấn đề liên quan