Get thumbnail Uri/path of the image stored in sd card + android

前端 未结 8 597
自闭症患者
自闭症患者 2020-11-28 04:46

SDK version - 1.6

I am using following intent to open android\'s default gallery:

Intent intent = new Intent();
                inte         


        
相关标签:
8条回答
  • 2020-11-28 04:55
    public static String getThumbnailPath(Context context, String path)
    {
      long imageId = -1;
    
      String[] projection = new String[] { MediaStore.MediaColumns._ID };
      String selection = MediaStore.MediaColumns.DATA + "=?";
      String[] selectionArgs = new String[] { path };
      Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);
      if (cursor != null && cursor.moveToFirst())
      {
        imageId = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
        cursor.close();
      }
    
      String result = null;
      cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(context.getContentResolver(), imageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
      if (cursor != null && cursor.getCount() > 0)
      {
        cursor.moveToFirst();
        result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
        cursor.close();
      }
    
      return result;
    }
    
    0 讨论(0)
  • 2020-11-28 04:56

    Based on @Karan's answer and following comments, just for the people that arrive here (like I did) and need a ready-to-work code:

    public String getThumbnailPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media._ID };
        String result = null;
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    
        cursor.moveToFirst();
        long imageId = cursor.getLong(column_index);
        cursor.close();
    
        cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(
                getContentResolver(), imageId,
                MediaStore.Images.Thumbnails.MINI_KIND,
                null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
            cursor.close();
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-11-28 05:06

    It could be a alternative ways as other had already mentioned in their answer but Easy way i found to get thumbnail is using ExifInterface

    ExifInterface exif = new ExifInterface(pictureFile.getPath());
    byte[] imageData=exif.getThumbnail();
    Bitmap  thumbnail= BitmapFactory.decodeByteArray(imageData,0,imageData.length);
    
    0 讨论(0)
  • 2020-11-28 05:07

    Two variants without depricated methods.

     public String getThumbnailPath(Uri uri) {
        String[] proj = { MediaStore.Images.Media.DATA };
    
        // This method was deprecated in API level 11
        // Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    
        CursorLoader cursorLoader = new CursorLoader(activity, uri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();
    
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    
        cursor.moveToFirst();
        long imageId = cursor.getLong(column_index);
        //cursor.close();
        String result="";
        cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(activity.getContentResolver(), imageId,
                MediaStore.Images.Thumbnails.MINI_KIND, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
            cursor.close();
        }
        return result;
    }
    public Bitmap getThumbnailBitmap(Uri uri){
        String[] proj = { MediaStore.Images.Media.DATA };
    
        // This method was deprecated in API level 11
        // Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    
        CursorLoader cursorLoader = new CursorLoader(activity, uri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();
    
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    
        cursor.moveToFirst();
        long imageId = cursor.getLong(column_index);
        //cursor.close();
    
        Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
                getContentResolver(), imageId,
                MediaStore.Images.Thumbnails.MINI_KIND,
                (BitmapFactory.Options) null );
    
        return bitmap;
    }
    
    0 讨论(0)
  • 2020-11-28 05:07

    Take a look at the following class

    http://developer.android.com/reference/android/provider/MediaStore.Images.Thumbnails.html

    0 讨论(0)
  • 2020-11-28 05:14

    The accepted answer is not working for me. I use following method to make it:

        try{
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), uri);
            Bitmap thumbBitmap = ThumbnailUtils.extractThumbnail(bitmap,120,120);
            // imageView.setImageBitmap(thumbBitmap);
        }
        catch (IOException ex){
            //......
        }
    
    0 讨论(0)
提交回复
热议问题