尝试从其他应用获取可绘制资源时内存不足

时间:2017-07-25 11:13:41

标签: android out-of-memory drawable android-resources

从我的应用程序中,我试图从其他已安装的应用程序中获取特定的可绘制资源。资源始终存在。我用:

Resources resources = activity.getPackageManager().getResourcesForApplication(packageName);
int previewResID = resources.getIdentifier("image", "drawable", packageName);
Drawable myThemePreview = resources.getDrawable(previewResID);

在drawable转换为位图并显示给用户

之前,我在最后一行得到了内存不足

1 个答案:

答案 0 :(得分:0)

您可以使用BitmapFactory::decodeResource使用带有指定inSampleSize的BitmapFactory.Options来减少内存。

它看起来像这样:

Resources resources = activity.getPackageManager().getResourcesForApplication(packageName);
int previewResID = resources.getIdentifier("image", "drawable", packageName);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeResource(resources,previewResId,options);