我有以下代码:
MemoryStream foo(){
MemoryStream ms = new MemoryStream();
// write stuff to ms
return ms;
}
void bar(){
MemoryStream ms2 = foo();
// do stuff with ms2
return;
}
Run Code Online (Sandbox Code Playgroud)
我分配的MemoryStream是否有可能以后不能被处理掉?
我有一个同行评审坚持我手动关闭它,我找不到信息来判断他是否有一个有效点.
假设我在一个位图对象中加载了一个图像
Bitmap myBitmap = BitmapFactory.decodeFile(myFile);
Run Code Online (Sandbox Code Playgroud)
现在,如果我加载另一个位图,会发生什么
myBitmap = BitmapFactory.decodeFile(myFile2);
Run Code Online (Sandbox Code Playgroud)
第一个myBitmap会发生什么?它是否收集垃圾或者我必须在加载另一个位图之前手动垃圾收集它,例如. myBitmap.recycle()?
此外,是否有更好的方法来加载大图像并在途中回收时一个接一个地显示它们?
我用这种方法来压缩图像
if(bitmapObject.compress(Bitmap.CompressFormat.PNG, 100, fOut))
{
...
}
Run Code Online (Sandbox Code Playgroud)
但是我得到的图像在压缩动作之前的尺寸要小得多(在尺寸上).
我的应用程序需要通过网络发送压缩图像 - 所以我想发送尽可能少的数据...但我必须保持图像的原始大小.
有没有其他方法来保持原始位图维度与一些压缩?
即使您已经尝试了一些减少内存使用的方法,捕获OutOfMemoryError是一个好习惯吗?或者我们应该不抓住异常?哪一个更好的做法?
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
bitmap = BitmapFactory.decodeFile(file, options);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在开发一款Android应用程序,它具有摄像头捕捉和照片上传功能.如果设备具有高分辨率相机,则捕获的图像尺寸将非常大(1~3MB或更多).
由于应用程序需要将此图像上传到服务器,因此我需要在上传之前压缩图像.例如,如果相机捕获了1920x1080全分辨率照片,则理想输出是保持图像的16:9比例,将其压缩为640x360图像以降低某些图像质量并使其以字节为单位缩小.
这是我的代码(从谷歌引用):
/**
* this class provide methods that can help compress the image size.
*
*/
public class ImageCompressHelper {
/**
* Calcuate how much to compress the image
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public 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 …Run Code Online (Sandbox Code Playgroud)