Android how to create runtime thumbnail

后端 未结 9 931
别那么骄傲
别那么骄傲 2020-11-27 09:59

I have a large sized image. At runtime, I want to read the image from storage and scale it so that its weight and size gets reduced and I can use it as a thumbnail. When a u

相关标签:
9条回答
  • 2020-11-27 10:33

    Use BitmapFactory.decodeFile(...) to get your Bitmap object and set it to an ImageView with ImageView.setImageBitmap().

    On the ImageView set the layout dimensions to something small, eg:

    android:layout_width="66dip" android:layout_height="48dip"
    

    Add an onClickListener to the ImageView and launch a new activity, where you display the image in full size with

    android:layout_width="wrap_content" android:layout_height="wrap_content"
    

    or specify some larger size.

    0 讨论(0)
  • 2020-11-27 10:39

    The best solution I found is the following. Compared with the other solutions this one does not need to load the full image for creating a thumbnail, so it is more efficient! Its limit is that you can not have a thumbnail with exact width and height but the solution as near as possible.

    File file = ...; // the image file
    
    Options bitmapOptions = new Options();
    bitmapOptions.inJustDecodeBounds = true; // obtain the size of the image, without loading it in memory
    BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);
    
    // find the best scaling factor for the desired dimensions
    int desiredWidth = 400;
    int desiredHeight = 300;
    float widthScale = (float)bitmapOptions.outWidth/desiredWidth;
    float heightScale = (float)bitmapOptions.outHeight/desiredHeight;
    float scale = Math.min(widthScale, heightScale);
    
    int sampleSize = 1;
    while (sampleSize < scale) {
        sampleSize *= 2;
    }
    bitmapOptions.inSampleSize = sampleSize; // this value must be a power of 2,
                                             // this is why you can not have an image scaled as you would like
    bitmapOptions.inJustDecodeBounds = false; // now we want to load the image
    
    // Let's load just the part of the image necessary for creating the thumbnail, not the whole image
    Bitmap thumbnail = BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);
    
    // Save the thumbnail
    File thumbnailFile = ...;
    FileOutputStream fos = new FileOutputStream(thumbnailFile);
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, fos);
    fos.flush();
    fos.close();
    
    // Use the thumbail on an ImageView or recycle it!
    thumbnail.recycle();
    
    0 讨论(0)
  • 2020-11-27 10:42

    Try this

    Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
    

    This Utility is available from API_LEVEl 8. [Source]

    0 讨论(0)
  • 2020-11-27 10:43

    My Solution

    byte[] imageData = null;
    
            try     
            {
    
                final int THUMBNAIL_SIZE = 64;
    
                FileInputStream fis = new FileInputStream(fileName);
                Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
    
                imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
    
                ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                imageData = baos.toByteArray();
    
            }
            catch(Exception ex) {
    
            }
    
    0 讨论(0)
  • 2020-11-27 10:43

    I found an easy way to do this

    Bitmap thumbnail = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(mPath),200,200)
    

    Syntax

    Bitmap thumbnail = ThumbnailUtils.extractThumbnail(Bitmap source,int width,int height)
    

    OR

    use Picasso dependancy

    compile 'com.squareup.picasso:picasso:2.5.2'

    Picasso.with(context)
        .load("file:///android_asset/DvpvklR.png")
        .resize(50, 50)
        .into(imageView2);
    

    Reference Picasso

    0 讨论(0)
  • 2020-11-27 10:44
    /**
     * Creates a centered bitmap of the desired size.
     *
     * @param source original bitmap source
     * @param width targeted width
     * @param height targeted height
     * @param options options used during thumbnail extraction
     */
    public static Bitmap extractThumbnail(
            Bitmap source, int width, int height, int options) {
        if (source == null) {
            return null;
        }
    
        float scale;
        if (source.getWidth() < source.getHeight()) {
            scale = width / (float) source.getWidth();
        } else {
            scale = height / (float) source.getHeight();
        }
        Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);
        Bitmap thumbnail = transform(matrix, source, width, height,
                OPTIONS_SCALE_UP | options);
        return thumbnail;
    }
    
    0 讨论(0)
提交回复
热议问题