如何逐个上传图像,以便每次成功下载图片上传后图像不会从屏幕上消失,内存会被清除? 试图在RecyclerView中加载少量图像。重要!图像从其他应用程序(我的应用程序的主题)加载。 这是代码:
private static final int MAX_SAMPLE = 16;
private static Drawable getDrawableFromResource(Resources themeRes, int resourceID)
{
if (resourceID == 0 || themeRes == null)
return null;
Drawable drawable = null;
Bitmap bitmap = null;
BitmapFactory.Options uniformOptions = new BitmapFactory.Options();
int inputSampleSize = uniformSampleSize;
if (inputSampleSize > MAX_SAMPLE)
inputSampleSize = MAX_SAMPLE;
String resName = getResName(resourceID);
for (int sizeDivider = inputSampleSize; sizeDivider <= MAX_SAMPLE; sizeDivider *= 2)
{
uniformOptions.inSampleSize = sizeDivider;
try
{ bitmap = BitmapFactory.decodeResource(themeRes, resourceID, uniformOptions); }
catch (OutOfMemoryError | Exception e)
{
Log.e(DynamicSettings.TAG_ERROR, "Load drawable: " + (resName != null ? resName : "name not found") + ", sizeDiv: " + sizeDivider);
e.printStackTrace();
continue;
}
try
{
drawable = new BitmapDrawable(bitmap);
return drawable;
}
catch (OutOfMemoryError | Exception e)
{
Log.e(DynamicSettings.TAG_ERROR, "Bad drawable resource file (parsing): " + resName);
e.printStackTrace(); // Out of memory or Null pointer
if (bitmap != null)
bitmap.recycle();
}
}
new BitmapDrawable(getErrorBitmap());
return new BitmapDrawable(getErrorBitmap());
}
如果所有图片都没有足够的内存 - 他们会使用inSampleSize uniformOptions调整大小 这是错误:
如果你调用bitmap.recycle()或类似的东西 - 来自RecyclerView的图像消失并抛出异常。