Android:根据原始图像的 Uri 获取 SD 卡上图像的缩略图

发布于 2024-10-16 06:51:58 字数 79 浏览 5 评论 0原文

所以我有一个图像的 Uri,用户从他的 SD 卡上的图像中选择。我想显示该图像的缩略图,因为显然该图像可能很大并占据整个屏幕。有人知道怎么做吗?

So i've got a Uri of an image the user chooses out of images off his SD card. And i'd like to display a thumbnail of that image, because obviously, the image could be huge and take up the whole screen. Anyone know how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冷情妓 2024-10-23 06:51:58

您可以使用 Java 的 ThumnailUtil 类简单地创建缩略图视频和图像

Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);


 public static Bitmap createVideoThumbnail (String filePath, int kind)

在 API 级别 8 中添加
为视频创建视频缩略图。如果视频损坏或格式不受支持,可能会返回 null。

参数
filePath 视频文件路径
kind 可以是 MINI_KIND 或 MICRO_KIND

了解 Thumbnail Util 类的更多源代码

Developer.android.com

You can simply create thumbnail video and image using ThumnailUtil class of java

Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);


 public static Bitmap createVideoThumbnail (String filePath, int kind)

Added in API level 8
Create a video thumbnail for a video. May return null if the video is corrupt or the format is not supported.

Parameters
filePath the path of video file
kind could be MINI_KIND or MICRO_KIND

For more Source code of Thumbnail Util class

Developer.android.com

趴在窗边数星星i 2024-10-23 06:51:58

此代码将完成这项工作:

Bitmap getPreview(URI uri) {
    File image = new File(uri);

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
        return null;

    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
            : bounds.outWidth;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
    return BitmapFactory.decodeFile(image.getPath(), opts);     
}

您可能需要计算最接近的 2 的幂以用于 inSampleSize,因为 据说更快。

This code will do the job:

Bitmap getPreview(URI uri) {
    File image = new File(uri);

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
        return null;

    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
            : bounds.outWidth;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
    return BitmapFactory.decodeFile(image.getPath(), opts);     
}

You may want to calculate the nearest power of 2 to use for inSampleSize, because it's said to be faster.

盗琴音 2024-10-23 06:51:58

我相信这段代码是从 SD 卡上的文件生成缩略图的最快方法:

 public static Bitmap decodeFile(String file, int size) {
    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, o);

    //Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = (int)Maths.pow(2, (double)(scale-1));
    while (true) {
        if (width_tmp / 2 < size || height_tmp / 2 < size) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale++;
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeFile(file, o2);
}

I believe this code is fastest way to generate thumbnail from file on SD card:

 public static Bitmap decodeFile(String file, int size) {
    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, o);

    //Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = (int)Maths.pow(2, (double)(scale-1));
    while (true) {
        if (width_tmp / 2 < size || height_tmp / 2 < size) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale++;
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeFile(file, o2);
}
樱娆 2024-10-23 06:51:58

尝试了几次后,我无法从 SD 中获取图像的缩略图路径。
在我在 gridview 适配器中(或您需要的地方)创建图像视图之前,我解决了获取 android 图像位图的问题。所以我调用方法 imageView.setImageBitmap(someMethod(Context context, imageID))

Bitmap someMethod(Context context, long imageId){

    Bitmap bitmap =   Media.Images.Thumbnails.getThumbnail(context.getAplicationContext.getContentResolver(), imageid, MediaStore.Images.Thumbnails.MINI_KIND, null);
    return bitmap;
}

您可以使用本指南从 SD 获取图像 ID (获取 Android 上的照片库列表)

A few trying I could not get the thumbnail path of image from SD.
i am resolved this problem getting an android image bitmap, before I create an image view in adapter for gridview (or where you need). So i call method imageView.setImageBitmap(someMethod(Context context, imageID))

Bitmap someMethod(Context context, long imageId){

    Bitmap bitmap =   Media.Images.Thumbnails.getThumbnail(context.getAplicationContext.getContentResolver(), imageid, MediaStore.Images.Thumbnails.MINI_KIND, null);
    return bitmap;
}

You can get image ID from your SD using this guide (Get list of photo galleries on Android)

笑叹一世浮沉 2024-10-23 06:51:58

如果您喜欢 HQ 缩略图,请使用 [RapidDecoder][1] 库。很简单:

import rapid.decoder.BitmapDecoder;
...
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image)
                             .scale(width, height)
                             .useBuiltInDecoder(true)
                             .decode();

如果您想缩小小于 50% 并获得 HQ 结果,请不要忘记使用内置解码器。
我在 API 级别 8 中测试了它:)

If you like HQ thumbnails, so use [RapidDecoder][1] library. It is simple as follow:

import rapid.decoder.BitmapDecoder;
...
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image)
                             .scale(width, height)
                             .useBuiltInDecoder(true)
                             .decode();

Don't forget to use builtin decoder if you want to scale down less than 50% and a HQ result.
I tested it in API Level 8 :)

菩提树下叶撕阳。 2024-10-23 06:51:58

该包将允许您访问图像 URI 以接收图像大小、大位图数据、将图像采样到任何较小的大小以节省内存并最大限度地提高性能。

它使用 InputStream 和 BitmapFactory:

public int[] getImageSize(Uri uri){
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        InputStream input = this.getContentResolver().openInputStream(uri);
        BitmapFactory.decodeStream(input, null, options); input.close();
        return new int[]{options.outWidth, options.outHeight};
    }
    catch (Exception e){}
    return new int[]{0,0};
}
public Bitmap BitmapImage(Uri uri){return BitmapImage(uri,-1,-1);}
public Bitmap BitmapImage(Uri uri, int maxSize){return BitmapImage(uri,maxSize,maxSize);}
public Bitmap BitmapImage(Uri uri, int Wmax, int Hmax){
    try {   
        InputStream input = this.getContentResolver().openInputStream(uri);
        double ratio=1;
        if ((Wmax>-1)&&(Hmax>-1)){
            int[] wh=getImageSize(uri); double w=wh[0], h=wh[1];
            if (w/Wmax>1){ratio=Wmax/w; if (h*ratio>Hmax){ratio=Hmax/h;}}
            else if (h/Hmax>1){ratio=Hmax/h;}
        }
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = (int)Math.ceil(1/ratio);
        bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
        input.close();
        return bitmap;
    }
    catch (Exception e){}
    return null;
}

针对不同用例的四个函数:

/*
    getImageSize(Uri uri): return int[]{ width, height}
    BitmapImage(Uri uri): return Bitmap in full size
    BitmapImage(Uri uri, int maxSize): return sampled Bitmap which is limited in square size
    BitmapImage(Uri uri, int Wmax, int Hmax): return sampled Bitmap which is limited width and height
*/

This package will let you access image URI to receive image size, large Bitmap data, sampling image to any smaller size for saving memory and maximize performance.

It uses InputStream and BitmapFactory:

public int[] getImageSize(Uri uri){
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        InputStream input = this.getContentResolver().openInputStream(uri);
        BitmapFactory.decodeStream(input, null, options); input.close();
        return new int[]{options.outWidth, options.outHeight};
    }
    catch (Exception e){}
    return new int[]{0,0};
}
public Bitmap BitmapImage(Uri uri){return BitmapImage(uri,-1,-1);}
public Bitmap BitmapImage(Uri uri, int maxSize){return BitmapImage(uri,maxSize,maxSize);}
public Bitmap BitmapImage(Uri uri, int Wmax, int Hmax){
    try {   
        InputStream input = this.getContentResolver().openInputStream(uri);
        double ratio=1;
        if ((Wmax>-1)&&(Hmax>-1)){
            int[] wh=getImageSize(uri); double w=wh[0], h=wh[1];
            if (w/Wmax>1){ratio=Wmax/w; if (h*ratio>Hmax){ratio=Hmax/h;}}
            else if (h/Hmax>1){ratio=Hmax/h;}
        }
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = (int)Math.ceil(1/ratio);
        bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
        input.close();
        return bitmap;
    }
    catch (Exception e){}
    return null;
}

Four functions for different use case:

/*
    getImageSize(Uri uri): return int[]{ width, height}
    BitmapImage(Uri uri): return Bitmap in full size
    BitmapImage(Uri uri, int maxSize): return sampled Bitmap which is limited in square size
    BitmapImage(Uri uri, int Wmax, int Hmax): return sampled Bitmap which is limited width and height
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文