android 从资源中获取位图或声音

发布于 2024-12-21 11:57:07 字数 615 浏览 4 评论 0原文

我需要从资产中获取位图和声音。我尝试这样做:

BitmapFactory.decodeFile("file:///android_asset/Files/Numbers/l1.png");

像这样:

getBitmapFromAsset("Files/Numbers/l1.png");
    private Bitmap getBitmapFromAsset(String strName) {
        AssetManager assetManager = getAssets();
        InputStream istr = null;
        try {
            istr = assetManager.open(strName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        return bitmap;
    }

但我只得到自由空间,而不是图像。

如何做到这一点?

I need to get Bitmap and sound from assets. I try to do like this:

BitmapFactory.decodeFile("file:///android_asset/Files/Numbers/l1.png");

And like this:

getBitmapFromAsset("Files/Numbers/l1.png");
    private Bitmap getBitmapFromAsset(String strName) {
        AssetManager assetManager = getAssets();
        InputStream istr = null;
        try {
            istr = assetManager.open(strName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        return bitmap;
    }

But I get just free space, not image.

How to do this?

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

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

发布评论

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

评论(6

月依秋水 2024-12-28 11:57:08

接受的答案永远不会关闭InputStream。以下是在资产文件夹中获取位图的实用方法:

/**
 * Retrieve a bitmap from assets.
 * 
 * @param mgr
 *            The {@link AssetManager} obtained via {@link Context#getAssets()}
 * @param path
 *            The path to the asset.
 * @return The {@link Bitmap} or {@code null} if we failed to decode the file.
 */
public static Bitmap getBitmapFromAsset(AssetManager mgr, String path) {
    InputStream is = null;
    Bitmap bitmap = null;
    try {
        is = mgr.open(path);
        bitmap = BitmapFactory.decodeStream(is);
    } catch (final IOException e) {
        bitmap = null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignored) {
            }
        }
    }
    return bitmap;
}

The accepted answer never closes the InputStream. Here is a utility method for getting a Bitmap in the assets folder:

/**
 * Retrieve a bitmap from assets.
 * 
 * @param mgr
 *            The {@link AssetManager} obtained via {@link Context#getAssets()}
 * @param path
 *            The path to the asset.
 * @return The {@link Bitmap} or {@code null} if we failed to decode the file.
 */
public static Bitmap getBitmapFromAsset(AssetManager mgr, String path) {
    InputStream is = null;
    Bitmap bitmap = null;
    try {
        is = mgr.open(path);
        bitmap = BitmapFactory.decodeStream(is);
    } catch (final IOException e) {
        bitmap = null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignored) {
            }
        }
    }
    return bitmap;
}
心如荒岛 2024-12-28 11:57:08

Kotlin 方式,自动关闭输入流。您必须自行处理异常。

private fun getBitmapFromAsset(context: Context, path: String): Bitmap =
        context.assets.open(path).use { BitmapFactory.decodeStream(it) }

Kotlin way, with auto close of the input stream. You have to handle exceptions on your own.

private fun getBitmapFromAsset(context: Context, path: String): Bitmap =
        context.assets.open(path).use { BitmapFactory.decodeStream(it) }
℡寂寞咖啡 2024-12-28 11:57:08

获取存储在 Assets 文件夹中的图像位图的方法。

       public static Bitmap getBitmapFromAssets(Context context, String fileName, int width, int height) {
        AssetManager assetManager = context.getAssets();

        InputStream istr;
        Bitmap bitmap = null;
        try {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            istr = assetManager.open(fileName);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, width, height);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeStream(istr, null, options);
        } catch (IOException e) {
            Log.e("hello", "Exception: " + e.getMessage());
        }

        return null;
    }

调整位图大小的方法。

 private static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

Method to get bitmap of image which is stored in Assets folder.

       public static Bitmap getBitmapFromAssets(Context context, String fileName, int width, int height) {
        AssetManager assetManager = context.getAssets();

        InputStream istr;
        Bitmap bitmap = null;
        try {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            istr = assetManager.open(fileName);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, width, height);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeStream(istr, null, options);
        } catch (IOException e) {
            Log.e("hello", "Exception: " + e.getMessage());
        }

        return null;
    }

Method to resize the bitmap.

 private static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
坚持沉默 2024-12-28 11:57:07
public static Bitmap getBitmapFromAsset(Context context, String filePath) {
    AssetManager assetManager = context.getAssets();

    InputStream istr;
    Bitmap bitmap = null;
    try {
        istr = assetManager.open(filePath);
        bitmap = BitmapFactory.decodeStream(istr);
    } catch (IOException e) {
        // handle exception
    }

    return bitmap;
}

该路径只是您的文件名 fx bitmap.png。如果您使用子文件夹 bitmap/ 则其 bitmap/bitmap.png

public static Bitmap getBitmapFromAsset(Context context, String filePath) {
    AssetManager assetManager = context.getAssets();

    InputStream istr;
    Bitmap bitmap = null;
    try {
        istr = assetManager.open(filePath);
        bitmap = BitmapFactory.decodeStream(istr);
    } catch (IOException e) {
        // handle exception
    }

    return bitmap;
}

the path is simply your file name fx bitmap.png. if you use subfolder bitmap/ then its bitmap/bitmap.png

影子的影子 2024-12-28 11:57:07

使用此代码其工作

try {
    InputStream bitmap=getAssets().open("icon.png");
    Bitmap bit=BitmapFactory.decodeStream(bitmap);
    img.setImageBitmap(bit);
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

更新

在解码位图时,如果图像尺寸很大,我们更经常会遇到内存溢出异常。因此,阅读文章如何有效地显示图像会对您有所帮助。

Use this code its working

try {
    InputStream bitmap=getAssets().open("icon.png");
    Bitmap bit=BitmapFactory.decodeStream(bitmap);
    img.setImageBitmap(bit);
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

Update

While decoding Bitmap we more often meet with memory overflow exception if Image size is very big. So reading article How to display Image efficiently will help you.

烙印 2024-12-28 11:57:07

简短的 Kotlin 版本:

assets
   .open(name)
   .use(BitmapFactory::decodeStream)

Short Kotlin version:

assets
   .open(name)
   .use(BitmapFactory::decodeStream)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文