How to find origID for getThumbnail when taking a

2019-07-15 12:13发布

问题:

For getThumbnail, the android documentation has:

public static Bitmap getThumbnail (ContentResolver cr, long origId, long groupId, int kind, BitmapFactory.Options options)

I have absolutely no idea how to get origId (The ID of the original image to perform getThumbnail on) when taking a picture with Camera.TakePicture.

My current attempt, based on various other questions I've read is:

String[] projection = { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA };
    String sort = MediaStore.Images.ImageColumns._ID + " DESC";
    Log.d("getting IDs:",sort);
    Cursor myCursor = managedQuery(imagesUri, projection, null, null, sort);
    myCursor.moveToFirst();
    thumbBitmap = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID)), MediaStore.Images.Thumbnails.MINI_KIND, null );

However, my log is outputting the string "_ID" for what should be the actual ID, and it then gives me a null pointer exception on the line where I try and create myCursor.

I also read as the answer to somebody else's similar question that images on the SD card don't have IDs, in which case I guess origID would actually be a URI and the docs are just messed up? I am extremely confused, and any explanation would be very very welcome.

回答1:

I ended up not being able to use getThumbnail, as I could not find any working way to use the path to the location of the image succsessfully, and (at the time at least, I believe there have been reports submitted) it had issues with devices not storing their thumbnails in the expected location.

My solution to this ended up being what I had hoped I could avoid, writing my own little thumbnail generator instead of using Android's getThumbnail.

public class CreateThumbnail extends Activity {
    Bitmap imageBitmap;
    public Bitmap notTheBestThumbnail(String file) {
        byte[] imageData = null;
        try     
        {

            final int THUMBNAIL_SIZE = 95;

            FileInputStream fis = new FileInputStream(file); //file is the path to the image-to-be-thumbnailed.
            imageBitmap = BitmapFactory.decodeStream(fis);
            imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 10, baos); //What image format and level of compression to use.
            imageData = baos.toByteArray();

        }
        catch(Exception ex) {
            Log.e("Something did not work", "True");
        }
        return imageBitmap;
    }   
}

I use the class like:

CreateThumbnail thumb = new CreateThumbnail();
thumb.notTheBestThumbnail(Environment.getExternalStorageDirectory() + "/exampleDir" + "/" + exampleVar  + "/example_img.jpg");
Bitmap mBitmap = thumb.imageBitmap; //Assigns the thumbnail to a bitmap variable, for manipulation.

While I didn't actually figure out how to get the ID, hopefully this will help anybody facing similar problems with getThumbnail.