在Android中显示SD卡中的大图像

发布于 2024-10-07 23:24:18 字数 176 浏览 0 评论 0原文

在 Android 中,如何显示 SD 卡中的图像(任意大小),不会出现内存不足错误

是否需要先将图片放入Media Store?

伪代码示例将不胜感激。 如果显示的图像与设备的内存水平一样大,则加分

In Android, how do you display an image (of any size) from the SD card, without getting an out of memory error?

Is it necessary to put the image in the Media Store first?

A pseudo-code example would be greatly appreciated. Extra points if the displayed image is as big as the memory level of the device allows.

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

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

发布评论

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

评论(4

铁憨憨 2024-10-14 23:24:18

编辑:这个问题实际上已经在 加载时出现奇怪的内存不足问题得到了解答图像到位图对象(得票最高的两个答案)。它也使用 inSampleSize 选项,但有一个小方法来自动获取适当的值。

我原来的答案:

BitmapFactory 的 inSampleSize .Options 类可以解决您的问题(http: //developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize)。它的工作原理是生成一个位图,其宽度和高度比原始值高 1/inSampleSize,从而减少内存消耗(通过 inSampleSize^2?)。您应该在使用之前阅读该文档。

示例:

BitmapFactory.Options options = new BitmapFactory.Options();
// will results in a much smaller image than the original
options.inSampleSize = 8;

// don't ever use a path to /sdcard like this, but I'm sure you have a sane way to do that
// in this case nebulae.jpg is a 19MB 8000x3874px image
final Bitmap b = BitmapFactory.decodeFile("/sdcard/nebulae.jpg", options);

final ImageView iv = (ImageView)findViewById(R.id.image_id);
iv.setImageBitmap(b);
Log.d("ExampleImage", "decoded bitmap dimensions:" + b.getWidth() + "x" + b.getHeight()); // 1000x485

但是,我猜它仅适用于 inSampleSize^2 倍允许内存大小的图像,并且会降低小图像的质量。
诀窍是找到合适的 inSampleSize。

Edit: this question has actually been already answered at Strange out of memory issue while loading an image to a Bitmap object (the two highest voted answers). It does also use the inSampleSize option, but with a small method to automatically get the appropriate value.

My original answer:

The inSampleSize of the BitmapFactory.Options class can solve your issue (http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize). It works by making a bitmap with the resulting width and height 1/inSampleSize than the original, thus reducing memory consumption (by inSampleSize^2?). You should read the doc before using it.

Example:

BitmapFactory.Options options = new BitmapFactory.Options();
// will results in a much smaller image than the original
options.inSampleSize = 8;

// don't ever use a path to /sdcard like this, but I'm sure you have a sane way to do that
// in this case nebulae.jpg is a 19MB 8000x3874px image
final Bitmap b = BitmapFactory.decodeFile("/sdcard/nebulae.jpg", options);

final ImageView iv = (ImageView)findViewById(R.id.image_id);
iv.setImageBitmap(b);
Log.d("ExampleImage", "decoded bitmap dimensions:" + b.getWidth() + "x" + b.getHeight()); // 1000x485

However here it will only work for images up to, I guess, inSampleSize^2 times the size of the allowed memory and will reduce the quality of small images.
The trick would be to find the appropriate inSampleSize.

旧瑾黎汐 2024-10-14 23:24:18

我使用代码显示任何尺寸的图像:

ImageView imageView=new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
FileInputStream fis=new FileInputStream(file);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=2; //try to decrease decoded image
options.inPurgeable=true; //if necessary purge pixels into disk
options.inScaled=true; //scale down image to actual device density
Bitmap bm=BitmapFactory.decodeStream(is, null, options);
imageView.setImageBitmap(bm);
fis.close();

I'm displaying any sized images using code:

ImageView imageView=new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
FileInputStream fis=new FileInputStream(file);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=2; //try to decrease decoded image
options.inPurgeable=true; //if necessary purge pixels into disk
options.inScaled=true; //scale down image to actual device density
Bitmap bm=BitmapFactory.decodeStream(is, null, options);
imageView.setImageBitmap(bm);
fis.close();
对不⑦ 2024-10-14 23:24:18

例如:

yourImgView.setImageBitmap(BitmapFactory.decodeFile("/sdcard/1.jpg"));

For example:

yourImgView.setImageBitmap(BitmapFactory.decodeFile("/sdcard/1.jpg"));
默嘫て 2024-10-14 23:24:18

http://www .developer.com/ws/other/article.php/3748281/Working-with-Images-in-Googles-Android.htm 涵盖了您需要了解的有关图像主题的所有信息,包括获取图像从 SD 卡。您会注意到执行此操作的代码,复制如下:

try {
   FileOutputStream fos = super.openFileOutput("output.jpg",
      MODE_WORLD_READABLE);

   mBitmap.compress(CompressFormat.JPEG, 75, fos);

   fos.flush();
   fos.close();
   } catch (Exception e) {
   Log.e("MyLog", e.toString());
}

http://www.developer.com/ws/other/article.php/3748281/Working-with-Images-in-Googles-Android.htm covers all you'll ever need to know on the subject of images, including getting them from an SD card. You'll notice the code to do so there, copied below:

try {
   FileOutputStream fos = super.openFileOutput("output.jpg",
      MODE_WORLD_READABLE);

   mBitmap.compress(CompressFormat.JPEG, 75, fos);

   fos.flush();
   fos.close();
   } catch (Exception e) {
   Log.e("MyLog", e.toString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文