【问题标题】:How to write an image (should be exact size) to an external storage如何将图像(应该是精确大小)写入外部存储
【发布时间】:2019-09-24 20:24:00
【问题描述】:

我正在编写一个程序,它从资源/drawable 中读取图像并刷新到外部存储。我的问题是不同的设备为同一个图像消耗不同的内存,但是我需要内存消耗保持不变。例如,设备 1 显示图像大小为 3MB,而设备 2 显示图像大小为 4MB。以下是我拥有的示例代码。 TIA

BitmapFactory.Options options = new BitmapFactory.Options();
//setting few options
//making sure that the getByteCount will always return the same value irrespective of the device
bm = BitmapFactory.decodeResource(res, R.drawable.image, options); 
bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream); //where outstream is where I need to store my image in the device filesystem

【问题讨论】:

    标签: java android image file-handling


    【解决方案1】:

    我可以假设您的设备具有不同的屏幕尺寸和密度。因此使用了不同的资源。

    为了获得特定屏幕尺寸的可绘制效果,您可以使用以下代码:

    Drawable drawable = resources.getDrawableForDensity(id, DisplayMetrics.DENSITY_XHIGH, theme);
    

    此外,您可以尝试缩小图像的大小:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inSampleSize = 3; 
    BitmapFactory.decodeResource(getResources(), R.mipmap.hqimage, options);
    

    但通常,您不能总是确定图像会消耗相同数量的内存,这取决于各种因素。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多